Reference: Jakarta Struts Live - Rick Hightower
Initialize1 Create a project directory, say myProj
2 Copy struts-blank.war into myProj
3 Extract struts-blank.war into myProj
4 Copy log4j-1.2.9.jar into myProj (for logging; optional)
5 Modify myProj/WEB-INF/src/build.xml to point to the jar file that contains the Servlet API (for ant; optional). Eg. <property name="servlet.jar" value="F:/Program Files/Tomcat-5.0.28/common/lib/servlet-api.jar"/lt;
6 Create a package in myProj/WEB-INF/src/java, say myPack
Write Action7 Write your Action in myPack, say MyAction.java, which extends Action and overrides the execute method:
package myPack;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class MyAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return mapping.findForward("success");
}
}
Write Forward8 Write you Forward in myProj, say myForward.jsp:
<html>
<head>
<title>My Action Was Successful!</title>
</head>
<body>
<h1> My Action Was Successful!</h1>
</body>
</html>
Configure Action and Forward9 Add the following to myProj/WEB-INF/struts-config.xml:
<action path="/myAction" type="myPack.MyAction">
<forward name="success" path="/myForward.jsp"/>
</action>
NOTEThe above associates the incoming path /myAction with the Action handler you wrote earlier, myPack.MyAction. Whenever this web application gets a request with /myAction.do (already done in myProj/WEB-INF/web.xml), the execute method of the myPack.MyAction class will be invoked.
Configure Action and Forward in web.xml (already done in struts-blank.war). web.xml uses struts-config.xml as the struts configuration file.
Build10 Modify build.xml to add the ${servlet.jar} file to the compile.classpath:
<pathelement path ="${servlet.jar}"/>
11 Modify build.xml to change the project.distname:
<property name="project.distname" value="myProj"/>
12 Modify build.xml to edit distpath.project:
<property name="distpath.project" value="F:/Program Files/Tomcat-5.0.28/webapps"/>
13 Run the ant script to get BUILD SUCCESSFUL:
C:\myProj\WEB-INF\src> ant
Deploy14 Start Tomcat and point your browser to http://localhost:8080/myProj/myAction.do
Logging (optional, but recommended)15 Add loj4j.properties to myProj\WEB-INF\src\java\:
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%5p] %d{mm:ss}(%F:%M:%L)%n%m%n%n
16 Edit MyAction.java:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
...
private static Log log = LogFactory.getLog(MyAction.class);
public ActionForward execute(...) throws Exception {
log.trace("In execute method of MyAction");
return mapping.findForward("success");
...
17 Edit build.xml to add logging to compile.classpath:
18 Edit log4j.propertiles to add:
log4j.logger.myProj=DEBUG
NOTE: Levels of logging: FATAL, ERROR, WARN, INFO, DEBUG, TRACE
Write ActionForm19 Create a class in myPack say MyForm that extends ActionForm and overrides reset, validate:
package myPack;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import javax.servlet.http.HttpServletRequest;
public class MyForm extends ActionForm {
private String firstName;
private Boolean registered;
public String getFirstName() {return firstName;}
public void setFirstName (String string) {firstName = string;}
public void reset(ActionMapping mapping,
HttpServletRequest request) {
firstName=null;
registered=false;
}
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (firstName==null || firstName.trim().equals("")){
errors.add("firstName",
new ActionError(
"userRegistration.firstName.problem"));
}
return errors;
}
}
20 Edit myProj\WEB-INF\src\java\resources\application.properties and add:
myProj.firstName.problem=The first name was blank
21 Create my.jsp in myProj:
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<html>
<head>
<title> My Project </title>
</head>
<body>
<h1>My Project</h1>
<html:errors/>
<table>
<html:form action="myProj">
<tr>
<td><bean:message key="myProj.firstName" />*</td>
<td><html:text property="firstName" /></td>
</tr>
<tr>
<td><html:submit /></td>
<td><html:cancel /></td>
</tr>
</html:form>
</table>
</body>
</html>
22 Modify struts.config.xml to add into form-beans:
<form-bean name="myForm" type="myPack.MyForm" />
23 Modify action so that it is:
<action path="/myProj" type="myPack.MyAction" name="myForm" input="/my.jsp">
<forward name="success" path="/regSuccess.jsp" />
</action>
24 Create labels for Form fields in application.properties (for internationalization) by adding:
myProj.firstName=First Name
25 Edit struts-config.xml to change:
<message-resources parameter="resources.application" />