ActionForm
An ActionForm is a JavaBean that extends
We will first create the class AddressForm which extends the ActionForm class. Here is the code of the class:
AddressForm.java
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class AddressForm extends ActionForm
{
private String name=null;
private String address=null;
private String emailAddress=null;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setAddress(String address){
this.address=address;
}
public String getAddress(){
return this.address;
}
public void setEmailAddress(String emailAddress){
this.emailAddress=emailAddress;
}
public String getEmailAddress(){
return this.emailAddress;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.name=null;
this.address=null;
this.emailAddress=null;
}
public ActionErrors validate(
ActionMapping mapping, HttpServletRequest request ) {
ActionErrors errors = new ActionErrors();
if( getName() == null || getName().length() < 1 ) {
errors.add("name",new ActionMessage("error.name.required"));
}
if( getAddress() == null || getAddress().length() < 1 ) {
errors.add("address",new ActionMessage("error.address.required"));
}
if( getEmailAddress() == null || getEmailAddress().length() < 1 ) {
errors.add("emailaddress",new ActionMessage("error.emailaddress.required"));
}
return errors;
}
AddressAction.java
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 AddressAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
return mapping.findForward("success");
}
}
Now we have to create an entry for form bean in the struts-config.xml. Add the following lines in the struts-config.xml file:
<form-bean
name="AddressForm"
type="com.ActionForm.AddressForm"/>
Add the following line in the struts-config.xml file for handling the action "/Address.do":
<action
path="/Address"
type="com.Action.AddressAction"
name="AddressForm"
scope="request"
validate="true"
input="/pages/Address.jsp">
<forward name="success" path="/pages/success.jsp"/>
</action>
Now create Address.jsp, which is our form for entering the address details. Code for Address.jsp is as follows:
Address.jsp
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html locale="true">
<head>
<title><bean:message key="welcome.title"/></title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="/Address">
<html:errors/>
<table>
<tr>
<td align="center" colspan="2">
<font size="4">Please Enter the Following Details</font>
</tr>
<tr>
<td align="right">
Name
</td>
<td align="left">
<html:text property="name" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td align="right">
Address
</td>
<td align="left">
<html:text property="address" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td align="right">
E-mail address
</td>
<td align="left">
<html:text property="emailAddress" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td align="right">
<html:submit>Save</html:submit>
</td>
<td align="left">
<html:cancel>Cancel</html:cancel>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>
An ActionForm is a JavaBean that extends
org.apache.struts.action.ActionForm. ActionForm maintains the session state for web application and the ActionForm object is automatically populated on the server side with data entered from a form on the client side.We will first create the class AddressForm which extends the ActionForm class. Here is the code of the class:
AddressForm.java
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class AddressForm extends ActionForm
{
private String name=null;
private String address=null;
private String emailAddress=null;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setAddress(String address){
this.address=address;
}
public String getAddress(){
return this.address;
}
public void setEmailAddress(String emailAddress){
this.emailAddress=emailAddress;
}
public String getEmailAddress(){
return this.emailAddress;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.name=null;
this.address=null;
this.emailAddress=null;
}
public ActionErrors validate(
ActionMapping mapping, HttpServletRequest request ) {
ActionErrors errors = new ActionErrors();
if( getName() == null || getName().length() < 1 ) {
errors.add("name",new ActionMessage("error.name.required"));
}
if( getAddress() == null || getAddress().length() < 1 ) {
errors.add("address",new ActionMessage("error.address.required"));
}
if( getEmailAddress() == null || getEmailAddress().length() < 1 ) {
errors.add("emailaddress",new ActionMessage("error.emailaddress.required"));
}
return errors;
}
AddressAction.java
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 AddressAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
return mapping.findForward("success");
}
}
Now we have to create an entry for form bean in the struts-config.xml. Add the following lines in the struts-config.xml file:
<form-bean
name="AddressForm"
type="com.ActionForm.AddressForm"/>
Add the following line in the struts-config.xml file for handling the action "/Address.do":
<action
path="/Address"
type="com.Action.AddressAction"
name="AddressForm"
scope="request"
validate="true"
input="/pages/Address.jsp">
<forward name="success" path="/pages/success.jsp"/>
</action>
Now create Address.jsp, which is our form for entering the address details. Code for Address.jsp is as follows:
Address.jsp
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html locale="true">
<head>
<title><bean:message key="welcome.title"/></title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="/Address">
<html:errors/>
<table>
<tr>
<td align="center" colspan="2">
<font size="4">Please Enter the Following Details</font>
</tr>
<tr>
<td align="right">
Name
</td>
<td align="left">
<html:text property="name" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td align="right">
Address
</td>
<td align="left">
<html:text property="address" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td align="right">
E-mail address
</td>
<td align="left">
<html:text property="emailAddress" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td align="right">
<html:submit>Save</html:submit>
</td>
<td align="left">
<html:cancel>Cancel</html:cancel>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>
No comments:
Post a Comment