Professional Documents
Culture Documents
J2EE
Struts
* This lecture was based on “Struts in Action” by Ted Husted ISBN 1-930110-50-2 Java II--Copyright © 2001-2003 Tom Hunter
Struts: Introduction
• Struts is based on the MVC or Model-View-Controller
design pattern.
ActionForm
ActionServlet
ActionMapping
ActionForward
Action
<HTML>
<HEAD><TITLE>Simple JSP</TITLE></HEAD>
<BODY>
<FORM METHOD="POST" ACTION="/TestServlet">
<BR>Username:
<INPUT TYPE="text" NAME="user" VALUE="">
<BR>Password:
<INPUT TYPE="text" NAME="pass" VALUE="">
</FORM>
</BODY>
</HTML>
Java II--Copyright © 2001-2003 Tom Hunter
Struts: ActionForm
• Given the HTML page we see below, we could pull out
the value of the tag with the NAME of user by using the
following code in a Servlet’s doPost() method:
<HTML>
<HEAD><TITLE>Simple JSP</TITLE></HEAD>
<BODY>
<FORM METHOD="POST" ACTION="/SomeServlet">
<BR>Username:
<INPUT TYPE="text" NAME="user" VALUE="">
<BR>Password:
<INPUT TYPE="text" NAME="pass" VALUE="">
</FORM>
</BODY>
</HTML>
Java II--Copyright © 2001-2003 Tom Hunter
Struts: ActionForm
• However, with any fairly large page, this becomes tedious.
public SimpleForm(){}
mapping.findForward( “somesymbolicname” );
import org.apache.struts.action.*;
• The names chosen for the components are not that meaningful and the
process is non-intuitive.
http://localhost:8080/register-complete/enter.jsp
UserName:
<input type="text" name="username" value=""><br>
enter password:
<input type="password" name="password1" value=""><br>
re-enter password:
<input type="password" name="password2" value=""><br>
<input type="submit" name="submit" value="Register">
</form>
Recall that the web container [the server] has its web.xml configuration
file. Within that config file, there is an element called—
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
—that allows you to specify that all pages with this pattern should go to the servlet
action.
Java II--Copyright © 2001-2003 Tom Hunter
Struts: Understanding the Architecture
3-7.) The ActionServlet looks in its
struts-config.xml for the mapping for that
particular path.
In this example, the pages directory will hold the JSP files. You have some
flexibility in where you place your JSPs, but wherever they go, you must make
sure your paths reflect that location.
web.xml
Welcome.jsp