CHAPTER 12
412
Migration Strategies
This chapter does not provide an exhaustive list of migration strategies and techniques. It only analyzes a
few very important ones. Most of the recommendations apply to HTML–based web applications, and don’t
necessarily hold good for applications that run in browser plug-ins.
Generating XML
As a first step, you need to explore replacing the HTML in web applications with a Flex user interface and
reusing everything up to the point where HTML generation starts. Most often the embedded script tags in
HTML, which are interpreted on the server, are dynamically replaced with HTML elements. These
embedded script tags could be JSP or PHP tags. This is how dynamic data is incorporated, while the
output remains plain HTML.
Although Flex applications can be made to consume HTML as text—and possibly you could write a parser
to glean the data from it—this technique is both inefficient and ugly. An elegant modification would be to
generate XML instead of HTML. Then you could apply an XSLT (http://www.w3.org/TR/xslt)
stylesheet to transform the XML output to an XHTML output. That way you still generate HTML (or more
accurately XHTML) while creating XML as an intermediate output. Flex applications can consume the
intermediate XML. So an old application gets refactored to work simultaneously with both HTML and Flex.
Figure 12-3 summarizes this technique in a diagram.
Figure 12-3. Generate XML. Consume XML in Flex and transform it to XHTML using an XSLT stylesheet.
MIGRATING WEB 1.0 INTERFACES TO RIA
413
Let’s take the earlier JSP example and make it generate XML. Here is the modified code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<%@ page contentType="text/xml;charset=ISO-8859-1" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%
List stockList = new ArrayList();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn =
DriverManager.getConnection
(//pass the connection credentials);
Statement stmt = conn.createStatement();
ResultSet rs =
stmt.executeQuery
("select id,symbol, name, price from Stocks");
while(rs.next()){
Map stock = new HashMap();
stock.put("id",rs.getInt(1));
stock.put("Symbol",rs.getString(2));
stock.put("name",rs.getString(3));
stock.put("price",rs.getString(4));
stockList.add(stock);
}
rs.close();
stmt.close();
conn.close();
%>
<%
for(int i=0;i<stockList.size();i++){
Map stock = (Map)stockList.get(i);
%>
<Stocks>
<stock><%=stock.get("id")%></stock>
<symbol><%=emp.get("symbol")%></symbol>
<name><%=emp.get("name")%></name>
<price><%=emp.get("price")%></price>
</Stocks>
<%
}
%>
The HTML elements are taken out and XML tags, including the XML declaration tag, are introduced into
the code. This JSP creates XML. An XSLT stylesheet can be applied to the XML to generate XHTML. I
won’t go into how that happens, because it’s not relevant to your purpose here.
Assuming this JSP is accessible via a URL, say http://localhost:8080/generateXML.jsp, it can be
consumed in Flex using the HTTPService component. Flex has rich capabilities for XML parsing. Its XML
capabilities can be utilized to bind the data effectively to the desired interfaces. The HTTPService code in
MXML could be as follows:
<s:HTTPService
id="xmlFromJSP"
url="http://localhost:8080/generateXML.jsp"
Get AdvancED Flex 4 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.