Java Tutorial - Java Script :
Building a Sample Application with Struts
private String mPassword = null;
public String getUserID()
{
return mUserID;
}
public void setUserID(String aUserID)
{
mUserID = aUserID;
}
public String getPassword()
{
return mPassword;
}
public void setPassword(String aPassword)
{
mPassword = aPassword;
}
public ActionErrors validate(
ActionMapping aMapping,
HttpServletRequest aR)
{
ActionErrors err = new ActionErrors();
if ((mUserID == null) || (mUserID.length() < 1))
err.add(“userid”, new ActionError(“error.userid”));
if ((mPassword == null) || (mPassword.length() < 1))
err.add(“password”, new ActionError(“error.password”));
return err;
}
public void reset(ActionMapping aMapping, HttpServletRequest aR)
{
mUserID = null;
mPassword = null;
}
}
The primary purpose of our ActionForm class is to transfer data from the input screen to our business logic. The class contains getter and setter methods for our data field, but the most important services are the validate() and reset() methods. The reset() method is called by the controller, just before transferring our data from the request object into our ActionForm object. After the data is transferred, validation is performed via the validate() method.
Next, we need to construct our OldFriendsAction class by extending the Struts Action class. Normally the OldFriendsAction class invokes classes that encapsulate the business logic for our application. In our case, we have not implemented any business logic so the OldFriendsAction class is left with just having to manage error-handling and control-forwarding duties. Chapter 14 provides a more detailed example that includes the business logic associated the Action class. Following is the source for our TOMCAT_ROOT/oldfriends/
WEB-INF/classes/OldFriendsAction class:
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class OldFriendsAction extends Action
{
public ActionForward execute(
ActionMapping aMapping,
ActionForm aForm,
HttpServletRequest aRequest,
HttpServletResponse aResponse)
throws ServletException
{
OldFriendsForm f = (OldFriendsForm) aForm;
String userid = f.getUserID();
String password = f.getPassword();
return aMapping.findForward(“main”);
}
}
The execute method of the Action class is where we would perform an operation such as authenticating our userID and password against a database before returning a Success or a Failure. ASuccess would cause the controller to forward to a view that would allow the user to continue his or her use of the application, a Failure would cause control to go back to the Login screen. The purpose of the Action class is to allow us to decouple the request from the model. This allows more than one request to use a given action. In our subclass OldFriendsAction, we retrieve our UserID and Password from the Form object and then use that data to authenticate the user. The opening screen for the application is created by a JSP file index.jsp. The source code for index.jsp is as follows:
<%@ taglib uri=”/WEB-INF/struts-html.tld” prefix=”html” %>
<%@ taglib uri=”/WEB-INF/struts-bean.tld” prefix=”bean” %>
<HTML>
<HEAD>
<TITLE><bean:message key=”oldfriends.highschool”/></TITLE>
</HEAD>
<BODY bgcolor=”#FFFFFF”>
<TABLE width=”100%” height=”100%” border=”1”>
<TBODY>
<TR>
<TD height=”20%” bgcolor=”#999999”>
<CENTER><H2>
<bean:message key=”oldfriends.highschool”/> Tigers
</H2></CENTER>
</TD>
</TR>
<TR>
<TD height=”80%”>
<TABLE width=”100%” height=”100%” border=”1”>
<TBODY>
<TR>
<TD height=”100%” width=”15%” bgcolor=”#999999”>
<TABLE border=”0” width=”138”>
<COL span=”1” align=”center” valign=”middle”>
<TBODY>
<TR>
<TD height=”27”>UserID</TD>
</TR>
<TR>
<TD height=”24”>
<html:form action=”oldfriends”>
<html:text property=”userID” />
</TD>
</TR>
<TR>
<TD height=”30”>Password</TD>
</TR>
<TR>
<TD height=”34”>
