Chapter 22: The Struts-Faces Integration Library

Using Struts Scripting with the Mini HR Application

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

  1. Add the Struts Scripting .jar files and properties file to the application.

  2. Convert SearchAction to a Groovy script.

  3. Configure Struts to use the new Groovy script–based action instead of the existing SearchAction in the struts-config.xml file.

  4. Repackage and run the updated application.

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

Add the Struts Scripting .jar Files and Properties File to the Application

The Struts Scripting .jar file must be added to the application in order to use Struts Scripting. Additionally, the BSF .jar file and the .jar file required for the Groovy scripting language must be added to the application. Finally, a Struts Scripting properties file must be added to the application. The properties file specifies any BSF non-default scripting languages being used and their associated script file extensions; in this case, Groovy.

The Struts Scripting and BSF .jar files must be copied from the Struts distribution (e.g., c:\java\struts-1.3.5\lib\struts-scripting-1.3.5.jar & c:\java\struts-1.3.5\lib\bsf-2.3.0.jar) to Mini HR's WEB-INF\lib directory. Next, download Groovy ( if you haven't already and copy the Groovy .jar file (e.g., c:\java\groovy-1.0-JSR-06\embeddable\groovy-all-1.0-JSR-06.jar) to Mini HR's WEB-INF\lib directory. The Struts Scripting properties file must be named and be accessible from the application's classloader (i.e., be placed on the application classpath). This file should be placed in Mini HR's WEB-INF\classes directory. The following code shows the properties file's content in its entirety.

struts-scripting.engine.groovy.class=org.codehaus.groovy.bsf.GroovyEngine struts-scripting.engine.groovy.extensions=gv,groovy

Convert SearchAction to a Groovy Script

The next step in using Struts Scripting with Mini HR is to replace the current Java-based SearchAction class with a Groovy script. Groovy is the scripting language used in this example because its syntax is very similar to Java. First, create a scripts directory underneath Mini HR's WEB-INF directory (i.e., WEB-INF\scripts). Placing the scripts directory under WEB-INF will protect scripts from being read directly from browsers. Next, create a file named Search.groovy with the contents shown next and place it in the newly created scripts directory.

import com.jamesholmes.minihr.EmployeeSearchService;     service = new EmployeeSearchService(); searchForm = struts.form;     // Perform employee search based on what criteria was entered. name =; if (name != null && name.trim().length() > 0) {   results = service.searchByName(name); } else {   results = service.searchBySsNum(searchForm.ssNum.trim()); }     // Place search results in SearchForm for access by JSP. searchForm.setResults(results);     // Forward control to this Action's input page. struts.setForward(struts.mapping.inputForward);

This Groovy script is essentially the same as the original SearchAction class but with some small abbreviations that the Groovy language enables. Note that the existing SearchAction class does not need to be removed from the application in order for Struts Scripting to work. It simply won't be referenced anymore now that there is a Groovy script replacement.

Configure the struts-config.xml File

After creating the Groovy script replacement for SearchAction, you must configure Struts to use it by updating Mini HR's Struts configuration file: struts-config.xml. The following snippet configures Struts to use the new Groovy script–based action:

<!-- Action Mappings Configuration --> <action-mappings>   <action path="/search"           type="org.apache.struts.scripting.ScriptAction"           parameter="/WEB-INF/scripts/Search.groovy"           name="searchForm"           scope="request"           validate="true"           input="/search.jsp">   </action> </action-mappings>

Notice that the type attribute of the action tag now specifies org.apache.struts.scripting. ScriptAction. ScriptAction is used to execute the script specified by the parameter attribute. The remaining attributes are unchanged from the original action definition used by Mini HR.

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"   "">     <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.jsp"/>   </global-forwards>       <!-- Action Mappings Configuration -->   <action-mappings>     <action path="/search"             type="org.apache.struts.scripting.ScriptAction"             parameter="/WEB-INF/scripts/Search.groovy"             name="searchForm"             scope="request"             validate="true"             input="/search.jsp">     </action>   </action-mappings>       <!-- Message Resources Configuration -->   <message-resources     parameter="com.jamesholmes.minihr.MessageResources"/>     </struts-config>

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, a file has been added and a file has 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 develop actions much faster and easier by using script-based actions that don't require compilation or server restarts.

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

Similar book on Amazon
Struts 2 in Action
Struts 2 in Action
Murach's Java Servlets and JSP, 2nd Edition
Murach's Java Servlets and JSP, 2nd Edition
Jakarta Struts For Dummies
Jakarta Struts For Dummies
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam © 2008-2017.
If you may any questions please contact us: