How do I set up a Struts 1 project?
Author: Deron Eriksson
Description: This Java tutorial walks through setting up a Struts 1 project in Eclipse.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


Page: < 1 2 3 >

(Continued from page 1)

Let's fire up our application and try this. After starting the struts-demo application in EclipseSW, we can open a browser window and go to http://localhost:8080/struts-demo.

http://localhost:8080/struts-demo

Remember that index.jsp is our welcome-file, so hitting http://localhost:8080/struts-demo will go to index.jsp. The index.jsp file forwards on to 'howdy', which goes to our 'myStart' action, which brings up our start.jsp, as shown below. Notice that the URL contains the action name, 'myStart.do', although the start.jsp is being hit, which we can see by the page title. Ignore the jsessionid for now (it is used for sessions).

http://localhost:8080/struts-demo/myStart.do

Now let's examine start.jsp. Notice the use of <bean:message key="my.message" />. In the screen capture, we can see that this tag produces the text 'This is my message'. Where is this value coming from? It comes from the StrutsDemoResource.properties file located at the root level of our src/ directory. StrutsW knows about this file because we have a mapping to this file in struts-config.xml, as seen here: <message-resources parameter="StrutsDemoResources" />.

start.jsp

<%@ 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://struts.apache.org/tags-nested" prefix="nested"%>
<html:html>
<head>
<title>start.jsp</title>
<html:base />
</head>
<body>
<p>Message: <bean:message key="my.message" /></p>
<a href="myTest.do?success=ok">Go to myTest Success</a>
<br/>
<a href="myTest.do">Go to myTest Failure</a>
</body>
</html:html>

The StrutsDemoResources.properties file is shown below. We can see that this file is a standard JavaSW properties file, and that the my.message key is associated with the 'This is my message' value.

StrutsDemoResources.properties

# This properties file holds resouces for the struts demo
my.message=This is my message

The next things to notice in start.jsp are the href links, href="myTest.do?success=ok" and href="myTest.do". If we examine our struts-config.xml file, we can see that the myTest.do action maps this time to the TestAction class, as seen here: <action path="/myTest" type="test.TestAction">.

In our Action class, we can perform some data processing or other tasks, and then decide where to go from there. In the TestAction class, we look for the presence of the 'success' parameter. If the parameter is present, the request gets forwarded on to 'success' via mapping.findForward("success"). Otherwise, the request gets forwarded to 'failure' via mapping.findForward("failure"). So, how does this determine where to go?

TestAction.java

package test;

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 TestAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String success = request.getParameter("success");
		if (success != null) {
			return mapping.findForward("success");
		} else {
			return mapping.findForward("failure");
		}
	}

}

If we look back in struts-config.xml, we can see that the mapping.findForward values map to the /success.jsp and /failure.jsp files.

	<action path="/myTest" type="test.TestAction">
		<forward name="success" path="/success.jsp"/>
		<forward name="failure" path="/failure.jsp"/>
	</action>

(Continued on page 3)

Page: < 1 2 3 >