• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Converting an application using xstl from struts to struts 2

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am working on converting a web application written in struts to struts 2. It uses xstl which I have never used but will need to convert to work in Struts 2. Can you point me to a tutorial on using xstl in struts 2?

Thanks.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain how the application uses XSLT.
 
Katie Doody
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It uses a xsl stylesheet that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes">
<xsl utput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:include href="header.xslt"/>
<xsl:template match="/EasementHistory">
<html>
<head>
<xsl:call-template name="header"/>
<title>Change history </title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="1355" height="286">
<!-- MSTableType="layout" -->
<tr>
<td valign="top" colspan="3" height="39">
<!-- MSCellType="ContentHead" -->
</td>
</tr>
<tr>
<td>
<xsl:text disable-output-escaping="yes">

</xsl:text>
</td>
<td valign="top">
<table class="table_standard">
<tbody>
<tr>
<td style="width:10%;"/>
<td>
<table class="table_standard">
<tbody>

<tr>
<td>
<h2> History Tracking of Easements</h2>
</td>
</tr>
<tr></tr>
<tr>
<td>
<xsl:choose>
<xsl:when test="count(/EasementHistory/History/Change) > 0">
<table class="table_standard">
<thead>
<tr>
<!--<th style="width=10%" />
<th>
</th> -->
<th style="width=100%" colspan="5"><span align="center" style= "color:#888888"> Job# : <xsl:value-of select="/EasementHistory/JobNbr"/></span>
</th>
<th/>
</tr>


<tr>
<th style="width=100%" colspan="5"></th>
</tr>
<tr class="odd_col_head" >
<th height="35" style=" width:11%">| Date |</th>
<th style="width:11%">| Field |</th>
<th style="width:33%">| Old value |</th>
<th style="width:33%">| New value | </th>
<th style="width:12%">| Change made by | </th>
</tr>
</thead>
<tbody>
<xsl:for-each select="/EasementHistory/History/Change">
<tr>
<xsl:if test="position mod 2 = 1">
<xsl:attribute name="class">data_row</xsl:attribute>
</xsl:if>
<xsl:if test="position() mod 2 = 0">
<xsl:attribute name="class">odd_col</xsl:attribute>
</xsl:if>
<td style="width:11%">
<xsl:value-of select="Date"/>
</td>
<td style= "width:11%">
<xsl:value-of select="Field"/>
</td>
<td style="width:31%">
<textarea rows="4" cols="40" readonly="readonly">
<xsl:if test="position mod 2 = 1">
<xsl:attribute name="class">data_row</xsl:attribute>
</xsl:if>
<xsl:if test="position mod 2 = 0">
<xsl:attribute name="class">odd_col</xsl:attribute>
</xsl:if>
<xsl:value-of select="OldValue"/>
</textarea>
</td>
<td style="width:31%">
<textarea rows="4" cols="40" readonly="readonly">
<xsl:if test="position mod 2 = 1">
<xsl:attribute name="class">data_row</xsl:attribute>
</xsl:if>
<xsl:if test="position mod 2 = 0">
<xsl:attribute name="class">odd_col</xsl:attribute>
</xsl:if>
<xsl:value-of select="NewValue"/>
</textarea>
</td>
<td style="width:15%">
<textarea rows="4" cols="25" readonly="readonly">
<xsl:if test="position mod 2 = 1">
<xsl:attribute name="class">data_row</xsl:attribute>
</xsl:if>
<xsl:if test="position mod 2 = 0">
<xsl:attribute name="class">odd_col</xsl:attribute>
</xsl:if>
<xsl:value-of select="ChangedBy"/>
</textarea>
</td>
</tr>
</xsl:for-each>
<tr><th height="25" colspan="5" width="100%"></th></tr>
<tr>
<td width="100%" colspan="5" align="center">
<input type="button"
onclick: self.close;return false;" value="Close"/>
</td>

</tr>
</tbody>
</table>
</xsl:when>
<xsl therwise>
<table class="table_standard">
<tbody>
<tr class="odd_col_head">
<th>There were no edits made to this easement</th>
</tr>
<tr><th height="25"></th></tr>

<tr>

<td align="center">
<input type="button" onclick: self.close;return false;" value="Close"/>
</td>





















</tr>
</tbody>
</table>
</xsl therwise>
</xsl:choose>
</td>
</tr>
</tbody>
</table>
</td>
<td style="width:15%"/>
</tr>
</tbody>
</table>
</td>
<td height="221">
<xsl:text disable-output-escaping="yes">

</xsl:text>
</td>
</tr>
<tr>
<td width="72">
<xsl:text disable-output-escaping="yes">

</xsl:text>
</td>
<td width="910">
<xsl:text disable-output-escaping="yes">

</xsl:text>
</td>
<td height="26" width="73">
<xsl:text disable-output-escaping="yes">

</xsl:text>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>












It gets serveral variables and xml from the database and returns this to the jsp page.

The jsp that look like this:

jsp page:
+"�?<%= com.mud.util.database.HistoryInfo.getXMLEasementHistory(request_id,"xslt/EasementHistory.xslt") %>

Here is the old procedure that returns the xml from the database:

//Displaying the easement history//
public static StringBuffer getXMLEasementHistory(int request_id, String xslt) throws NamingException, SQLException{
System.out.println("HistoryCheck_3");
String sql = "select req_id, job_nbr,request_nbr from pe_request where req_id ="+request_id;
StringBuffer xml = new StringBuffer();
Connection con = DBConn.getConnection();
if((con == null) || (con.isClosed())){
Context initContext = new InitialContext();
Context envCtx = (Context)initContext.lookup("java:/comp/env");
String dbuser = (String)envCtx.lookup("user");
String dbpass = (String)envCtx.lookup("password");
String url = (String)envCtx.lookup("ConnectDB");
DBConn dbcon = new DBConn(dbuser,dbpass,url);
con = DBConn.getConnection();
}
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
String job_nbr = "";
String request_nbr="";
while(rs.next()){
job_nbr = rs.getString("job_nbr");
request_nbr=rs.getString("request_nbr");
}

xml.append("�?<?xml-stylesheet type=\"text/xsl\" href=\""+xslt+ "\"?>\n");
xml.append("�?<EasementHistory>");
xml.append("�?<ReqId>"+request_id+"�?</ReqId>");
xml.append("�?<JobNbr>"+job_nbr+", Request# - "+request_nbr+"�?</JobNbr>");
java.text.SimpleDateFormat datetime_format = new java.text.SimpleDateFormat("MM/dd/yyyy");
xml.append("�?<History>");
sql = "SELECT field_name, old_value, new_value, updated_by , date_of_update FROM pe_HISTORY WHERE easement_id is not null and req_id = "+request_id+"order by date_of_update";
rs = stmt.executeQuery(sql);
while(rs.next()){
xml.append("�?<Change>");
xml.append("�?<Field>" + rs.getString("field_name")+
"�?</Field>");
String old_val=null;
if(rs.getString("old_value")==null)
old_val=" ";
else
old_val=rs.getString("old_value");
xml.append("�?<OldValue>" +old_val+
" �?</OldValue>");
xml.append("�?<NewValue>" + rs.getString("new_value")+
"�?</NewValue>");
xml.append("�?<ChangedBy>" + rs.getString("updated_by")+
"�?</ChangedBy>");
xml.append("�?<Date>" + datetime_format.format(new java.util.Date(rs.getDate("date_of_update").getTime()))+
"�?</Date>");
xml.append("�?</Change>");
}
xml.append("�?</History>");
xml.append("�?</EasementHistory>");
return xml;
} logger.error("Exception in getFormParamInfo");
throw ce;
}catch(Exception e){
logger.error("Exception in getFormParamInfo", e);
throw new EASException("EAS_EXEC_001");
}finally{
easDBUtil.closeDBResources(con, pstmt, null, rs);
}
return xml;
}



So, what I have done so far in the new application is create a procedure in my dao that returns the xml with the variables. I have added the xslt (changed to xsl) to my application changing the path to the xsl in the xml to match. I have then created a .jsp page that displays the xml as a variable.

Here is my new procedure:

public StringBuffer getEasementHistory(String xslt, int reqId, String jobNbr, String reqNbr)throws EASException{
StringBuffer xml = new StringBuffer();
EASDBUtil easDBUtil = new EASDBUtil(logger);
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {
xml.append("�?<?xml-stylesheet type=\"text/xsl\" href=\""+xslt+ "\"?>\n");
xml.append("�?<EasementHistory>");
xml.append("�?<ReqId>"+reqId+"�?</ReqId>");
xml.append("�?<JobNbr>"+jobNbr+", Request# - "+reqNbr+"�?</JobNbr>");
java.text.SimpleDateFormat datetime_format = new java.text.SimpleDateFormat("MM/dd/yyyy");
xml.append("�?<History>");
con = easDBUtil.getConnection();
pstmt = con.prepareStatement(EASDBConstants.GET_HISTORY);
pstmt.setInt(1,reqId);
rs = pstmt.executeQuery();
if(rs != null){
while(rs.next()){
xml.append("�?<Change>");
xml.append("�?<Field>" + rs.getString("field_name")+
"�?</Field>");
String old_val=null;
if(rs.getString("old_value")==null)
old_val=" ";
else
old_val=rs.getString("old_value");
xml.append("�?<OldValue>" +old_val+
" �?</OldValue>");
xml.append("�?<NewValue>" + rs.getString("new_value")+
"�?</NewValue>");
xml.append("�?<ChangedBy>" + rs.getString("updated_by")+
"�?</ChangedBy>");
xml.append("�?<Date>" + datetime_format.format(new java.util.Date(rs.getDate("date_of_update").getTime()))+
"�?</Date>");
xml.append("�?</Change>");
}
xml.append("�?</History>");
xml.append("�?</EasementHistory>");
}
}catch(EASException ce){
logger.error("Exception in getFormParamInfo");
throw ce;
}catch(Exception e){
logger.error("Exception in getFormParamInfo", e);
throw new EASException("EAS_EXEC_001");
}finally{
easDBUtil.closeDBResources(con, pstmt, null, rs);
}
return xml;
}



My struts action looks like this:


<action name="QueryEditEasements" class="com.mud.eas.action.EASQueryEditEasementsA">
<result>/pages/queryEditEasementsPage.jsp</result>
<result name="SUCCESS1">/pages/multipleEasementsPage.jsp</result>
<result type="xslt" name="SUCCESS2">pages/historyPage.jsp</result>
</action>
Thanks for your help.

[ June 25, 2008: Message edited by: Katie Doody ]
[ June 25, 2008: Message edited by: Katie Doody ]
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method below can be called from a Struts action. If you design the stylesheet to produce a JSP page, you should be good.
 
Katie Doody
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your reply. Is this procedure something that works in Struts or Struts2? If I was not going to use a jsp, would it be any different. I am confused as to why you would use a jsp and a stylesheet. From what I have read, it would seem to be one or the other?

Thanks.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you very much for your reply. Is this procedure something that works in Struts or Struts2? If I was not going to use a jsp, would it be any different. I am confused as to why you would use a jsp and a stylesheet. From what I have read, it would seem to be one or the other?



The method above can be called from a Struts action. The version number is not important.

I only mentioned having the stylesheet output a JSP page because it seemed that you needed a JSP page. If you don't need a JSP page then just have the stylesheet output basic HTML (no JSP).

With Struts Actions, you have access to the HttpReponse and can do whatever you want with it. Most programmers stay within the bounds of Struts and return the ActionForward and let the ActionServlet handle the response. But, if needed, such as in the case of sending XML, you can generate your own reponse and return null to the ActionServlet.

It is a best practice to create an application-specific base action class which extends the Struts Action class. Then, all of your actions should extend the base class rather than the Struts Action class. The method above would go in the base class. So, your action would be to (1) forward control to the Model and get the XML file, and (2) create the XSL response and send HTML to browser.
[ June 26, 2008: Message edited by: James Clark ]
 
Katie Doody
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay thanks, I know I asked about the jsp because that was the way the old application was working. I was just wondering because from what I was reading it sounded like stylesheet would replace the jsp.

I have the application doing everything you suggest but know I am not sure how to configure my struts.xml (using Struts2) because I see configuration like this :

<result name="SUCCESS2">/pages/historyPage.jsp</result>

or like this

<result name="SUCCESS2" type="xslt>
<param name="location">/WEB-INF/dummyStrutsTransformation.xsl</param>
</result>
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use the design that I indicate, it does not matter what you put in the configuration file. The response will be sent directly from the action.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic