Struts Study

Struts is introduced as MVC (Model-View-Control) framework. Struts implement View and Control. In this document, I put two programs side by side. Both use MVC design pattern. Both do identical task. One is JSP + Servlet; we call it traditional method, another is Struts. In our example, the screen display are identical, but the tags are different. The big different between the traditional and Struts are the Controls. In traditional method, Control is Servlet class file. In Struts, it is called Action Servlet, which consist of two elements, one is Action Form bean, another is Action.

  Struts Trditional
View Struts tags, JSP tags JSP tags
Control ActionForm bean, Action class Servlet class.

Following are the file structure diagram and the complete source code. Source code is fully tested.

Initial page  
Struts (struts_form.jsp) Tradition JSP (form.jsp)

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<html:html>
<HEAD>

<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>

<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<TITLE>struts_form.jsp</TITLE>
</HEAD>

<BODY>

<html:form action="/register">

<p><font size="+1">Sample Model II Design <br>
</font><font size="+1" color="#ff0000">Using Struts Technology </font></p>
<table width="500" border="0" cellspacing="0" cellpadding="2">
<tr>
<td>Username</td>
<td><html:text property="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><html:text property="password" /></td>
</tr>
<tr>
<td>Re-Enter Password </td>
<td><html:text property="repassword" /></td>
</tr>
<tr>
<td>Preference</td>
<td><html:select property="selcolor" >
<html:options property="colorList" />
</html:select></td>
</tr>
<tr>
<td></td>
<td><html:submit property="submit" value="Submit" /> <html:reset /></td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
</html:form>
</BODY>
</html:html>


<html>
<head>
<title>Model II, JSP + Servlet Technology</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="ProcessForm">
<p><font size="+1">Sample Model II Design </font><br>
<font size="+1" color="#0000ff">Using JSP + Servlet Technology </font></p>
<table width="500" border="0" cellspacing="0" cellpadding="2">
<tr>
<td>Username</td>
<td><input name="username" type="text" id="username" value="Please enter username"></td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td>Re-Enter Password </td>
<td><input name="repassword" type="text" id="repassword"></td>
</tr>
<tr>
<td>Preference</td>
<td><select name="selcolor" id="color">
<option>Red</option>
<option>Yellow</option>
<option>Magenta</option>
<option selected>No Color</option>
</select></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
</form>
</body>
</html>

Servlet page

 
Struts Form bean (RegisterForm.java) and
Action (RegisterAction.java)
Tradition Servlet (ProcessForm.java)

//Form Bean
//RegisterForm.java
package compstruts.forms;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
* Form bean for a Struts application.
* @version 1.0
* @author
*/
public class RegisterForm extends ActionForm {

protected String username = "Please enter username" ;
protected String password ;
protected String repassword ;
protected String selcolor = "No Color" ;
protected String[] colorList={"Red", "Yellow", "Magenta", "No Color"};

public String getPassword() {
return password;
}
public String getRepassword() {
return repassword;
}
public String getSelcolor() {
return selcolor;
}
public String getUsername() {
return username;
}
public void setPassword(String string) {
password = string;
}
public void setRepassword(String string) {
repassword = string;
}
public void setSelcolor(String string) {
selcolor = string;
}
public void setUsername(String string) {
username = string;
}
public String[] getColorList() {
return colorList;
}
public void setColorList(String[] strings) {
colorList = strings;
}

}

package servlet;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @version 1.0
* @author
*/
public class ProcessForm extends HttpServlet implements Servlet {

/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
doPost( req, resp);
}

/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String username = req.getParameter("username");
String password = req.getParameter("password");
String repassword = req.getParameter("repassword");
String selcolor = req.getParameter("selcolor");

if(password.equals(repassword)==true)
{
req.getRequestDispatcher("/success.jsp").forward(req, resp);
}
else
{
resp.sendRedirect("failed.htm");
}
}
}

//Action
//RegisterAction.java
package compstruts.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import compstruts.forms.RegisterForm;

/**
* @version 1.0
* @author
*/
public class RegisterAction extends Action {

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward();
// return value
RegisterForm rf = (RegisterForm) form;

String username=rf.getUsername() ;
String password = rf.getPassword() ;
String repassword = rf.getRepassword() ;
String selcolor = rf.getSelcolor() ;

if(password.equals(repassword)==true)
{
forward = mapping.findForward("success");
}
else
{
forward = mapping.findForward("failure");
}

// Finish with
return (forward);

}
}

 
Struts configuration, which link the initial page, Form bean, and action together (struts_config.xml). Not applicable
<?xml version="1.0" encoding="UTF-8"?>
<!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>

<!-- Data Sources -->
<data-sources>
</data-sources>

<!-- Form Beans -->
<form-beans>
<form-bean name="registerForm" type="compstruts.forms.RegisterForm">
</form-bean>
</form-beans>

<!-- Action Mappings -->
<action-mappings>
<action name="registerForm" path="/register" scope="request" type="compstruts.actions.RegisterAction" input="/struts_form.jsp">
<forward name="success" path="/struts_success.jsp">
</forward>
<forward name="failure" path="/failed.htm">
</forward>
</action>
</action-mappings>

<!-- Message Resources -->
<message-resources parameter="compstruts.resources.ApplicationResources"/>

</struts-config>

 
Response Page , Success  
Struts (strut_seccuss.jsp).
Note: Can use simple JSP page (success.jsp) too.
Simple JSP page (success.jsp)
<HTML>
<HEAD>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<TITLE>struts_success.jsp</TITLE>
</HEAD>
<BODY>
<p>Success !
</p>
<p>You have entered</p>
<!--To retrieve value back, you can use one of following two ways.-->
<!--1. save registerForm in session -->
<!--2. or you need add Form tag to let property know where to find the registerFrom bean -->
<html:form action="/register">
<p>Username: <font color="#FF0000"><bean:write name = "registerForm" property="username" /> </font></p>
<p>Password: <font color="#FF0000"><bean:write name = "registerForm" property="password" /></font> </p>
<p>Reentered password: <font color="#FF0000"><bean:write name = "registerForm" property="repassword"/></font> </p>
<p>Preference: <font color="#FF0000"><bean:write name = "registerForm" property = "selcolor" /></font> </p>
</html:form>
<P><BR><a href="index.htm">Go back to beginning</a>
</P>
</BODY>
</HTML>
<%
//Assume the Servlet page using Forwarding. The values are passed through request.
//Get values back and display them

String username = request.getParameter("username");
String password = request.getParameter("password");
String repassword = request.getParameter("repassword");
String selcolor = request.getParameter("selcolor");
%>

<html>
<head>
<title>Success</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>Success !
</p>
<p>You have entered</p>
<p>Username: <font color="#FF0000"><%=username %> </font></p>
<p>Password: <font color="#FF0000"><%=password %></font> </p>
<p>Reentered password: <font color="#FF0000"><%=repassword %></font> </p>
<p>Preference: <font color="#FF0000"><%=selcolor %></font> </p>
<P><BR><a href="index.htm">Go back to beginning</a>
</P>
</body>
</html>

Response Page , Failure. This is not a very important page in this example. I use the same HTML for both (failed.htm) .
<html>
<head>
<title>Failure</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Failing !
<BR>
<BR>
<A href="index.htm">Go back to beginning</A>

</body>
</html>

Conclustion:

4/11/2004

Good Struts sample (include error handling):

http://www.webagesolutions.com/knowledgebase/waskb/waskb003/