Converting the Mini HR Application to Use Modules

 < Day Day Up > 



Now that you've seen the benefits of using Strut's module mechanism and how it works, you are ready to update the Mini HR application to use modules. Here are the steps that you will follow:

  1. Set up module directories and files.

  2. Create a Struts configuration file for each module.

  3. Update the application's web.xml deployment descriptor file.

  4. Repackage and run the updated application.

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

Set Up Module Directories and Files

The first step in updating the Mini HR application is to set up a directory for each module that will store its JSPs. This example will have two modules, an employee module and a reports module. The employee module will contain functionality previously existing in Mini HR. The reports module adds new functionality. You need to create a directory for each of these modules beneath the main Mini HR application directory (e.g., c:\java\MiniHR). Thus, you will have the following directories:

c:\java\MiniHR\employee c:\java\MiniHR\reports 

After you have created the directories, you need to put the corresponding JSPs into them. Because the original Mini HR application already has employee functionality, you can simply move the employee search JSP (search.jsp) from the root application directory (e.g., c:\java\MiniHR) to the new employee module directory you just created. The reports module contains new functionality, which is provided by the menu.jsp file, shown here. Store this file in the reports module directory.

<html> <head> <title>ABC, Inc. Human Resources Portal - Reports Menu</title> </head> <body> <font size="+1"> ABC, Inc. Human Resources Portal - Reports Menu </font><br> <hr width="100%" noshade="true"> &#149; New Employee Report<br> &#149; 5-Year Employee Report<br> &#149; 10-Year Employee Report<br> &#149; 20-Year Employee Report<br> </body> <html>

After you move the search.jsp file and create the menu.jsp file, your main application directory should look like this:

C:\java\MiniHR\employee\search.jsp c:\java\MiniHR\reports\menu.jsp c:\java\MiniHR\index.jsp

Before moving on, a short, but important, digression is in order. The general idea behind the directory structure just described can be extended to configuration files, if you like. For example, you could create a directory for each module beneath /WEB-INF/ that would hold the module's Struts, Tiles, and Validator configuration files. Following is an example directory layout illustrating this:

MiniHR/WEB-INF/moduleA/struts-config.xml MiniHR/WEB-INF/moduleA/tiles-defs.xml MiniHR/WEB-INF/moduleA/validation.xml MiniHR/WEB-INF/moduleB/struts-config.xml MiniHR/WEB-INF/moduleB/tiles-defs.xml MiniHR/WEB-INF/moduleB/validation.xml

Create a Struts Configuration File for Each Module

After you have set up the module directories and files, the next step is to create a Struts configuration file for each of the modules. The application's current struts-config.xml file will be used as the configuration file for the employee module, and thus must be renamed to struts-config-employee.xml. Following is the renamed and updated struts-config-employee.xml file:

<?xml version="1.0"?> <!DOCTYPE struts-config PUBLIC   "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"   "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config>   <!-- Form Beans Configuration -->   <form-beans>     <form-bean name="searchForm"                type="com.jamesholmes.minihr.SearchForm"/>   </form-beans>   <!-- Action Mappings Configuration -->   <action-mappings>     <action path="/viewSearch" forward="/search.jsp"/>     <action path="/search"             type="com.jamesholmes.minihr.SearchAction"             name="searchForm"             scope="request"             validate="true"             input="/search.jsp">     </action>   </action-mappings>   <!-- Message Resources Configuration -->   <message-resources     parameter="com.jamesholmes.minihr.ApplicationResources"/> </struts-config>

In addition to being renamed, this file has also been updated. Notice that the Global Forwards Configuration section has been removed and the /viewSearch action has been added.

As mentioned earlier in this chapter, Struts incorporates the concept of a default module. The default module's configuration file is named struts-config.xml and is shown next:

<?xml version="1.0"?> <!DOCTYPE struts-config PUBLIC   "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"   "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config>   <!-- Global Forwards Configuration -->   <global-forwards>     <forward name="viewSearch" path="/employee/viewSearch.do"/>   </global-forwards>   <!-- Message Resources Configuration -->   <message-resources     parameter="com.jamesholmes.minihr.ApplicationResources"/> </struts-config>

You will need to create this new file and place it under the /WEB-INF/ directory (e.g., c:\java\MiniHR\WEB-INF). This configuration file simply has a forward pointing to the Search page that is used by index.jsp for its Search for Employees link.

The reports module's configuration file, struts-config-reports.xml, is shown next:

<?xml version="1.0"?> <!DOCTYPE struts-config PUBLIC   "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"   "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config>   <!-- Action Mappings Configuration -->   <action-mappings>     <action path="/viewMenu" forward="/menu.jsp"/>   </action-mappings>   <!-- Message Resources Configuration -->   <message-resources     parameter="com.jamesholmes.minihr.ApplicationResources"/> </struts-config> 

You will also need to create this new file and place it under the /WEB-INF/ directory (e.g., c:\java\MiniHR\WEB-INF). This configuration file simply has a forward action pointing to the Reports Menu page that is used by index.jsp for its Reports Menu link.

Update the Application's web.xml Deployment Descriptor File

Once you have created the Struts configuration files for each module, you must update Mini HR's web.xml deployment descriptor file. Each module's configuration file will be passed to Strut's ActionServlet controller servlet as an initialization parameter, as shown next:

<!-- 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>   <init-param>     <param-name>config/employee</param-name>     <param-value>/WEB-INF/struts-config-employee.xml</param-value>   </init-param>   <init-param>     <param-name>config/reports</param-name>     <param-value>/WEB-INF/struts-config-reports.xml</param-value>   </init-param>   <load-on-startup>1</load-on-startup> </servlet>

Each initialization parameter specifies a name and a value for the parameter with the param-name and param-value tags, respectively. The default module's configuration file is specified with a parameter named config. Each of the module-specific configuration files is specified with a parameter named config/moduleName, where moduleName is the name of the module the configuration file is for.

Following is the updated web.xml file in its entirety:

<?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>     <init-param>       <param-name>config/employee</param-name>       <param-value>/WEB-INF/struts-config-employee.xml</param-value>     </init-param>     <init-param>       <param-name>config/reports</param-name>       <param-value>/WEB-INF/struts-config-reports.xml</param-value>     </init-param>     <load-on-startup>1</load-on-startup>   </servlet>   <!-- Action Servlet Mapping -->   <servlet-mapping>     <servlet-name>action</servlet-name>     <url-pattern>*.do</url-pattern>   </servlet-mapping>   <!-- The Welcome File List -->   <welcome-file-list>     <welcome-file>/index.jsp</welcome-file>   </welcome-file-list>   <!-- Struts Tag Library Descriptors -->   <taglib>     <taglib-uri>/WEB-INF/tlds/struts-bean.tld</taglib-uri>     <taglib-location>/WEB-INF/tlds/struts-bean.tld</taglib-location>   </taglib>   <taglib>     <taglib-uri>/WEB-INF/tlds/struts-html.tld</taglib-uri>     <taglib-location>/WEB-INF/tlds/struts-html.tld</taglib-location>   </taglib>   <taglib>     <taglib-uri>/WEB-INF/tlds/struts-logic.tld</taglib-uri>     <taglib-location>/WEB-INF/tlds/struts-logic.tld</taglib-location>   </taglib> </web-app> 

Repackage and Run the Updated Application

Because no Java source code files had to be modified to update the Mini HR application to use modules, you do not have to recompile the application. All you have to do is repackage the application and redeploy it before running it again. Assuming that you have 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:

jar cf MiniHR.war *

This command should be run from the directory where you have set up the Mini HR application (e.g., c:\java\MiniHR).

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 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 application running, you will see the screen shown in Figure 9-2.

click to expand
Figure 9-2: The opening Mini HR application screen

Clicking the Search for Employees link takes you to the same employee search page that you were taken to by clicking the same link in the original application. The Reports Menu link takes you to the new menu.jsp page created for the reports module.



 < Day Day Up > 



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

Similar book on Amazon

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