Reducing the Number of ActionForms

Reducing the Number of ActionForms

Previous Home Next

 

Reducing the Number of ActionForms

Struts 1.3 framework provide the ActionForm class to dynamically generate the form then display and store the data which are given by the user if the application developer who are not developed more then one ActionForm in the web application in java than struts 1.3 framework also provide the DynaActionForm this ActionForm dynamically generate the ActionForm at the run time.

 

Directory Structure of ActionFormExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head>
<title><bean:message key="label.title"/></title>       
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<html:form action="/actionFormAction" >
<table cellpadding="3" cellspacing="3">
<tr><td><bean:message key="label.name"/></td><td><html:text name="actionFormForm" property="name" /></td></tr>
<tr><td><bean:message key="label.address"/></td><td><html:text name="actionFormForm" property="address" /></td></tr>
<tr><td><bean:message key="label.city"/></td><td><html:text name="actionFormForm" property="city" /></td></tr>
<tr><td><bean:message key="label.state"/></td><td><html:text name="actionFormForm" property="state" /></td></tr>
<tr><td><bean:message key="label.country"/></td><td><html:text name="actionFormForm" property="country" /></td></tr>
<tr><td><html:submit><bean:message key="label.submit"/></html:submit></td></tr>
</table>
</html:form>
</body>
</html>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Maven Struts Examples</display-name> 
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>
         /WEB-INF/struts-config.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet> 
  <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping> 
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <form-beans>
  <form-bean name="actionFormForm" type="org.r4r.struts.MyActionForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings>
  <action path="/actionForm" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/>
  <action path="/actionFormAction" name="actionFormForm" type="org.r4r.struts.MyActionFormAction" input="/index.jsp">
  <forward name="success" path="/success.jsp"/>
  </action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

MyActionForm.java

package org.r4r.struts;

import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class MyActionForm extends ActionForm {
	private String name;
	private String address;
	private String city;
	private String state;
	private String country;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	

}

MyActionFormAction.java

package org.r4r.struts;

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 MyActionFormAction extends Action {
	public ActionForward execute(ActionMapping mapping,ActionForm form,
	HttpServletRequest request,HttpServletResponse response) 
	throws Exception{
	return mapping.findForward("success");
	}
}

ApplicationResources.properties

# Resources for parameter 'org.r4r.struts.ApplicationResources'
label.title=Struts 1.3 ActionForm Example
label.header=Struts 1.3 ActionForm Example
label.name=Name
label.address=Address
label.city=City
label.state=State
label.country=Country
label.submit=Submit

success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head>
<title><bean:message key="label.title"/></title>       
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<table cellpadding="3" cellspacing="3" border="1">
<tr><td><bean:message key="label.name"/>:-</td><td><bean:write name="actionFormForm" property="name"/></td></tr>
<tr><td><bean:message key="label.address"/>:-</td><td><bean:write name="actionFormForm" property="address"/></td></tr>
<tr><td><bean:message key="label.city"/>:-</td><td><bean:write name="actionFormForm" property="city"/></td></tr>
<tr><td><bean:message key="label.state"/>:-</td><td><bean:write name="actionFormForm" property="state"/></td></tr>
<tr><td><bean:message key="label.country"/>:-</td><td><bean:write name="actionFormForm" property="country"/></td></tr>
</table>
</body>
</html>

Output




Previous Home Next