Struts

adplus-dvertising
Application-4 : Creating a Simple DynaForm Example with Struts
Previous Home Next

DynaBeans are the extensibility and flexibility according to the incoming request, defining even the simplest JavaBean requires defining a new class and coding a field and two methods for each property. The property of DynaBean can be configured via an XML descriptor, its property can't called by a simple java method but work well with reflection and introspection component.

In this chapter, we have create a very simple DynaForm Example with struts. This Example design in NetBeans IDE- 6.8 with Struts 1.3.

Requirement for your DynaForm Example-

  1. At least one view component is required in every Application.
  2. A Subclass of DynaActionForm class which play virtual role of ActionForm class.
  3. A subclass of Action class which provide specific processing functionality for the application.
  4. A web.xml file is required for calling ActionServlet and struts-config.xml.
  5. A struts-config.xml file is required for updating and mapping the update.

Now, Following the following steps for develop your Fourth application name as DynaFormExample. DynaFormExample application contain following page-

  1. Four View file name as index.jsp, welcomeStruts.jsp,and success.jsp
  2. Since, we used DynaForm so no used to make a bean class. As we define earlier about DynaForm
  3. one Action file name as LoginAction.java, used for provide specific processing functionality.
  4. Two XML file name as web.xml and struts-config.xml file, used for provide control, mapping and validation in application.

Step- 1: Start your IDE (NetBeans 6.8 with struts 1.3).

Step- 2: Open a new project( Crtl+Shift+N), name it , add server( Apache Tomcat 6.0.20 ), then add struts framework 1.3 , and don't forget to add Struts TLDs.

Note:-When you click Finish button, NetBeans IDE put and configure everything in place and project open in your IDE.

Step- 3: Open welcomeStruts.jsp page from your struts application and code it as describe below.

<%-- Document: welcomeStruts.jsp
Description : Second Page of onLineDraw Application--%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean"
prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html"
prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic"
prefix="logic" %>
<html:html lang="true">
<head>
<meta http-equiv="Content-Type" content="text/html; 
charset=UTF-8">
<title><bean:message key="welcome.title"/></title>
<html:base/>
</head>
<body style="background-color: white">
<logic:notPresent name="org.apache.struts.action.MESSAGE" 
scope="application">
<div  style="color: red">
ERROR:  Application resources not loaded -- check servlet
container logs for error messages.
</div>
</logic:notPresent>
<html:form action="/example" method="post">
<h3><bean:message key="welcome.heading"/></h3>
<p><bean:message key="welcome.message"/></p>
<table style="border:groove" border="1" cellspacing="5" 
cellpadding="5">
<tbody>
<tr>
<html:errors property="first" />
<td> <bean:message key="welocme.first" /></td>
<td><html:text property="first" size="20"/></td>
</tr>
<tr>
<html:errors property="last" />
<td><bean:message key="welcome.last" /> </td>
<td><html:text property="last" size="20"/></td>
</tr>
<tr>
<html:errors property="age" />
<td><bean:message key="welcome.age" /></td>
<td><html:text property="age" size="20"/></td>
</tr>
<tr>
<html:errors property="number" />
<td><bean:message key="welocme.number" /></td>
<td><html:text property="number" size="20"/></td>
</tr>
<tr>
<td align="center"><html:submit property="submit" 
value=" Submit " /></td>
<td align="center"><html:reset property="reset" 
value=" Reset " /></td>
</tr>
</tbody>
</table>
</html:form>
</body>
</html:html> 

Step- 4: Open success.jsp page from your struts application and code it as describe below.

<%-- Document   : success.jsp
Created on : Third Page of onLineDraw Application--%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://struts.apache.org/tags-bean"
prefix="bean" %>
<html>
<head>
<meta http-equiv="refresh" content="10; 
URL=index.jsp">
<title>success Page</title>
</head>
<body>
<h1>Your Detail!</h1>
<ul>
<p> First Name:<strong> <bean:write name="DynaExample"
property="first" /></strong> </p>
<p> Last Name: <strong><bean:write name="DynaExample" 
property="last" /></strong></p>
<p> Age: <strong><bean:write name="DynaExample" 
property="age" /></strong></p>
<p> Number: <strong><bean:write name="DynaExample"
property="number" /></strong></p>
</ul>
<ul>
<p> Please, wait page automatic return to <strong>
Home </strong> Page. </p>
</ul>
</body>
</html>

Step- 6: Open struts-config.xml file place in WEB-INF folder, this file is used to map Model class, Exception class, Validation class, Action class( Action class further choose View class) and ApplicationResource class.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts 
Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds
/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<!-- DynaForm configure into struts-config.xml 
and provide the parameter -->
<form-bean name="DynaExample" type="org.apache
.struts.action.DynaActionForm" >
<form-property name="first" type="java.lang.String"/>
<form-property name="last" type="java.lang.String"/>
<form-property name="age" type="java.lang.Integer"/>
<form-property name="number" type="java.lang.Integer"/>
</form-bean>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="welcome"  path="/Welcome.do"/>
</global-forwards>
<!-- all the action relative stuff map here:-->
<action-mappings>
<action input="/welcomeStruts.jsp" name="DynaExample"
path="/example" scope="request" type="r4r.struts.ExampleAction">
<forward name="success" path="/success.jsp"/>
<forward name="failure" path="/welcomeStruts.jsp"/>
</action>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>
<controller processorClass="org.apache.struts.tiles
.TilesRequestProcessor"/>
<message-resources parameter="com/myapp/struts
/ApplicationResource"/>
<!-- ======== Tiles plugin ========-->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" 
value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
<!-- ======== Validator plugin ========-->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>

Step- 7: Open ApplicationResource.properties place in com.myapp.struts package, this file act as resource bundle of application and used to provide the property and validation into application.

Step- 8: Since we Already Add property into DynaForm form struts-config.xml file. so no need to design Bean class. Action class automatic call all the property from DynaFrom.

Step- 9: DynaForm directly access the Action class of servlet by its instance.

// Save as a ExampleAction.java
package r4r.struts;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class ExampleAction extends org.apache
	.struts.action.Action {
/* forward name="success" path="" */
private static final String SUCCESS = "success";
/* failure name="failure" path="" */
private static final String FAILURE = "failure";
@Override
public ActionForward execute(ActionMapping mapping,
ActionForm form,HttpServletRequest request, 
HttpServletResponse response)
throws Exception {
//Add error into incoming HTTP request
ActionErrors error = new ActionErrors();
// By making instance of DynaActionForm class, 
DynaActionForm actionForm = (DynaActionForm) form;
//Value store into DynaForm is call here:
String first = (String) actionForm.get("first");
String last = (String) actionForm.get("last");
int age = (Integer) actionForm.get("age");
int number = (Integer) actionForm.get("number");

//Check the valiadation condition into incomming 
HTTP request form DynaForm
if (first == null || first.length() < 1) {
error.add("first", new ActionMessage("error.first.required"));
}
if (last == null || last.length() < 1) {
error.add("last", new ActionMessage("error.last.required"));
}
if (age == 0) {
error.add("age", new ActionMessage("error.age.required"));
}
if (number < 1) {
error.add("number", new ActionMessage("error.number.required"));
}
/*Save Error/request and dispaly it anywhere within 
this application */
saveErrors(request, error);
if (error.isEmpty()) {
return mapping.findForward(SUCCESS);
} else {
return mapping.findForward(FAILURE);
}
}
}

Run your application:

Note:-Now your application is complete, so now start run your application. Start your server( which you embedded when configure our struts application), then right click your application and click upon Run option or open your welcome( index.jsp) page and press shift+F6.

Note:-Check the Validation into Example

Previous Home Next