Chapter 23: Using AJAX with Struts


Using the Struts-Faces Library with the Mini HR Application

Now that you've seen the details of using the Struts-Faces library, you are ready to update the Mini HR application to use the library. Following is the list of steps that you follow to use the Struts-Faces library with the Mini HR application:

  1. Add a JSF implementation's and JSTL .jar files to the application.

  2. Add a servlet definition for the JSF controller servlet to the web.xml file.

  3. Configure Struts to use a custom Struts-Faces request processor in the struts-config.xml file. Also, update forward definitions in the struts-config.xml file.

  4. Update existing JSPs to use the Struts-Faces and JSF tag library tags.

  5. Repackage and run the updated application.

The following sections walk you through each step of the process in detail.

Add a JSF Implementation's and JSTL .jar Files to the Application

A JSF implementation's .jar files must be added to the application in order to use JSF functionality. Any JSF implementation that has been certified to be compatible with the JSF specification will work, such as the reference implementation from Sun or the Apache MyFaces implementation. Additionally, JSTL .jar files must be added to the application for Struts-Faces to function.

Assuming you're using the reference implementation from Sun, you must copy the jsf-api.jar and jsf-impl.jar files from the reference implementation's lib directory (e.g., c:\java\ jsf-1_1_01\lib) to Mini HR's WEB-INF\lib directory. Next, copy the JSTL .jar files packaged with the Struts distribution (e.g., c:\java\struts-1.3.5\lib\jstl-1.0.2.jar & c:\java\struts-1.3.5\ lib\standard-1.0.2.jar) to Mini HR's WEB-INF\lib directory.

Add a Servlet Definition for the JSF Controller Servlet to the web.xml File

Similar to the setup for the Struts controller servlet, a servlet definition and related configuration details must be added to the web.xml file for the JSF controller servlet. The following snippet shows the configuration details that must be added to the web.xml file:

<!-- JSF Servlet Configuration --> <servlet>   <servlet-name>faces</servlet-name>   <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>   <load-on-startup>1</load-on-startup> </servlet>     <!-- JSF Servlet Mapping --> <servlet-mapping>   <servlet-name>faces</servlet-name>   <url-pattern>*.faces</url-pattern> </servlet-mapping>

In addition to adding the JSF controller servlet configuration details to the web.xml file, the Struts servlet configuration details must be modified so that the JSF controller servlet is initialized before the Struts controller servlet. Recall that the order in which servlets are initialized is controlled by the <load-on-startup> tag. The <load-on-startup> tag specifies a priority value for the order in which servlets are loaded. Servlets with a higher priority (lower value) are loaded first. The JSF controller servlet priority is set to 1, so the Struts controller servlet must be updated to a priority of 2 so that it loads after the JSF controller servlet, as shown here:

<!-- Action Servlet Configuration --> <servlet>   <servlet-name>action</servlet-name>   <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>   <init-param>     <param-name>config</param-name>     <param-value>/WEB-INF/struts-config.xml</param-value>   </init-param>   <load-on-startup>2</load-on-startup> </servlet>

The following code shows the updated web.xml file for Mini HR in its entirety. The sections that have changed or that have been added are shown in bold.

<?xml version="1.0"?>     <!DOCTYPE web-app PUBLIC   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"   "http://java.sun.com/dtd/web-app_2_3.dtd">     <web-app>       <!-- Action Servlet Configuration -->   <servlet>     <servlet-name>action</servlet-name>     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>     <init-param>       <param-name>config</param-name>       <param-value>/WEB-INF/struts-config.xml</param-value>     </init-param>     <load-on-startup>2</load-on-startup>   </servlet>       <!-- Action Servlet Mapping -->   <servlet-mapping>     <servlet-name>action</servlet-name>     <url-pattern>*.do</url-pattern>   </servlet-mapping>       <!-- JSF Servlet Configuration -->   <servlet>     <servlet-name>faces</servlet-name>     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>     <load-on-startup>1</load-on-startup>   </servlet>       <!-- JSF Servlet Mapping -->   <servlet-mapping>     <servlet-name>faces</servlet-name>     <url-pattern>*.faces</url-pattern>   </servlet-mapping>       <!-- The Welcome File List -->   <welcome-file-list>     <welcome-file>/index.jsp</welcome-file>   </welcome-file-list>     </web-app> 

Configure the struts-config.xml File

After you have updated the web.xml file, you must configure Struts to use a custom Struts-Faces request processor by updating Mini HR's Struts configuration file: struts-config.xml. The following snippet configures Struts to use a Struts-Faces request processor:

<!-- Controller Configuration --> <controller>   <set-property property="processorClass"     value="org.apache.struts.faces.application.FacesRequestProcessor"/> </controller>

Using the Struts-Faces request processor causes Struts to recognize requests for pages utilizing JSF and to handle them appropriately.

In addition to configuring the Struts-Faces request processor, you must update each of the forward and action definitions in the Struts configuration file that point to JSPs that make use of JSF components. Without Struts-Faces, forward and action definitions point directly to JSPs. With Struts-Faces, they point to the page's JSF URI. For example, before, the search action pointed directly to search.jsp, as shown here:

<action path="/search"         type="com.jamesholmes.minihr.SearchAction"         name="searchForm"        scope="request"     validate="true"        input="/search.jsp"/>

However, now the action will point to the search page's JSF URI, as shown here:

<action path="/search"         type="com.jamesholmes.minihr.SearchAction"         name="searchForm"        scope="request"     validate="true"        input="search.faces"/>

At run time, the Struts-Faces library will determine if a specified page is the path of a JSF URI or an actual path to a page. If a JSF URI is specified, then Struts-Faces will process the page accordingly; otherwise, normal Struts processing will take place.

The following code shows the updated Struts configuration file for Mini HR in its entirety. The sections that have changed or that have been added are shown in bold.

<?xml version="1.0"?>     <!DOCTYPE struts-config PUBLIC   "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"   "http://struts.apache.org/dtds/struts-config_1_3.dtd">     <struts-config>       <!-- Form Beans Configuration -->   <form-beans>     <form-bean name="searchForm"                type="com.jamesholmes.minihr.SearchForm"/>   </form-beans>       <!-- Global Forwards Configuration -->   <global-forwards>     <forward name="search" path="/search.faces"/>   </global-forwards>       <!-- Action Mappings Configuration -->   <action-mappings>     <action path="/search"             type="com.jamesholmes.minihr.SearchAction"             name="searchForm"             scope="request"             validate="true"             input="/search.faces">     </action>   </action-mappings>       <!-- Controller Configuration -->   <controller>     <set-property property="processorClass"       value="org.apache.struts.faces.application.FacesRequestProcessor"/>   </controller>       <!-- Message Resources Configuration -->   <message-resources     parameter="com.jamesholmes.minihr.MessageResources"/>     </struts-config>

Update Existing JSPs to Use the Struts-Faces and JSF Tag Library Tags

The next step is to update the application's original JSPs to use Struts-Faces and JSF Tag Library tags instead of the Struts Tag Library tags. To do this, you must replace each of the Struts tags that have JSF replacements in the original page. Following is the updated search.jsp page using the JSF tags. Each section of the file that is new or has been changed is shown in bold.

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>         <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>     <%@ taglib uri="http://struts.apache.org/tags-faces" prefix="s" %>     <f:view>     <html> <head> <title>ABC, Inc. Human Resources Portal - Employee Search</title> </head> <body>         <font size="+1"> ABC, Inc. Human Resources Portal - Employee Search </font><br> <hr width="100%" noshade="true">         <s:errors/>     <s:form  action="/search">     <table> <tr> <td align="right"><s:message key="label.search.name"/>:</td> <td><h:inputText  value="#{searchForm.name}"/></td> </tr> <tr> <td></td> <td>-- or --</td> </tr> <tr> <td align="right"><s:message key="label.search.ssNum"/>:</td> <td><h:inputText  value="#{searchForm.ssNum}"/> (xxx-xx-xxxx)</td> </tr> <tr> <td></td> <td><h:commandButton value="Submit"/></td> </tr> </table>         </s:form>     <logic:present name="searchForm" property="results">         <hr width="100%" size="1" noshade="true">         <bean:size  name="searchForm" property="results"/> <logic:equal name="size" value="0"> <center><font color="red"><cTypeface:Bold>No Employees Found</b></font></center> </logic:equal>         <logic:greaterThan name="size" value="0"> <table border="1"> <tr> <th>Name</th> <th>Social Security Number</th> </tr> <logic:iterate  name="searchForm" property="results"> <tr> <td><bean:write name="result" property="name"/></td> <td><bean:write name="result" property="ssNum"/></td> </tr> </logic:iterate> </table> </logic:greaterThan>     </logic:present>    </body> <html>     </f:view> 

As you can see, the Struts Tag Library tags were replaced with Struts-Faces and JSF tags. The Struts-Faces tags enable the rest of your Struts application to stay unchanged and continue to work as it did before.

Repackage and Run the Updated Application

Because no Java code was modified during this process, it's not necessary to recompile the Mini HR application. However, several files have been modified, so the application needs to be repackaged and redeployed before it is run. Assuming you've made modifications to the original Mini HR application and it was set up in the c:\java\MiniHR directory (as described in Chapter 2), the following command line will repackage the application when run from c:\java\MiniHR:

jar cf MiniHR.war * 

Similar to the way you ran Mini HR the first time, you now need to place the new MiniHR.war file that you just created into Tomcat's webapps directory, delete the webapps/MiniHR directory, and start Tomcat. As before, to access the Mini HR application, point your browser to http://localhost:8080/MiniHR/. Once you have the updated Mini HR running, everything should work as it did before. However, now you can use JSF to create advanced component-based user interfaces.



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2004
Pages: 165
Authors: James Holmes

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net