This example is designed to explain how to configure and use dynamic form bean, here we will design a registration process to demonstrate this where the user will be first presented with a registration page(Registration.jsp) which allows to submit the user details to the application(RegisterAction.java) that are inserted into data base. The following listing shows the Registration.jsp

Registration.jsp

<html><body>
<html:form action=”register”><pre>
UserName: <html:text property=”uname” size=”25”/><br>
Password:<html:password property=”pass” size=”25”/><br>
Email       :<html:text property=”email” size=”25”/><br>
Address:<html:text property=”address” size=”25”/><br>
Mobile :<html:text  property=”mobile” size=”25”/><br>
<html:submit>Register</html:submit>
</pre><html:form>
</body></html>

The following listing shows Home.jsp

<html><body>
<b>welcome</b><bean:write name=”registerform” property=”uname”/>
</body></html>

The following listing shows the RegisterAction.java which demonstrates how to read the data from the DynaActionForm

RegisterAction.java

Package com.chandra.struts;
Import org.apache.struts.action.*;
Import javax.servlet.http.*;
Import java.sql.*;
Public class RegisterAction extends Action{
Public RegisterAction(){
Try{Class.forName(“oracle.jdbc.driver.OracleDriver”);}
Catch(Exception e){}
}//cons
Public ActionForward execute(ActionMapping am,ActionForm af,HttpServletRequest req,
                                                HttpServletResponse res)throws Exception{
DynaActionForm daf=(DynaActionForm)af;
Connection con=null;
Try{
Con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”scott”,”tiger”);
PreparedStatement ps = con.prepareStatement(“insert inot userdetails values(?,?,?,?,?,?)”);
Ps.setString(1,(String)daf.get(“uname”));
Ps.setString(2,(String)daf.get(“Pass”));
Ps.setString(3,(String)daf.get(“email”));
Ps.setString(4,(String)daf.get(“address”));
Ps.setInt(5,(Integer)daf.get(“mobile”)).intValue());
Ps.setString(6,”user”);
If(ps.executeUpdate()==1)
Return am.findForwrd(“success”);
}//try
Finally{con.close();}
Return am.findForward(“fail”);
}//execute
}//class

Struts-config.xml

<struts-config>
 <form-beans>
   <form-bean name=”registerform” type=”org.apache.struts.action.DynaActionForm”>
   <form-property name=”uname” type=”java.lang.String”/>
   <form-property name=”pass” type=”java.lang.String”/>
   <form-property name=”email” type=”java.lang.String”/>
   <form-property name=”address” type=”java.lang.String”/>
   <form-property name=”mobile” type=”java.lang.Integer”/>
   </form-bean>
</form-beans>
<action-mappings>
 <action path=”/register” type=”com.chandra.struts.RegisterAction” name=”registerform”
            Scope=”session” validate=”false”>
    <forward name=”success” path=”/Home.jsp”/>
    <forward name=”fail” path=”/Registraction.jsp”/>
 </action>
</action-mappings>
</struts-config>

Web.xml

<web-app>
<servlet>
            <servlet-name>as</servlet-name>
            <servler-class>org.apache.struts.action.ActionServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
            <servlet-name>as</servlet-name>
            <url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

After writing the files in the above listings compile the RefisterAction.java and arrange application stretcher and now copy the dynaBeanEX1 (if you application folder name) folder in<tomcat home>/webapps folder and start the server. Browser the application using the the following url http://localhost:8080/dynaBeanEx1/Registeration.jsp and enter the details . After submitting the form the registration details are inserted into the database and the response is presented as welcome username.

Limitation of DynaActionForm

With DynaActionForm we cannot use programmatic validation i.e., server side validation implemented in form beans validate method. However, this is not a major problem since we have declarative validations that can be used to validate the DynaActionForm fields. Obser the above example, if can be identified that the form data is submitted to the RefisterAction without undergoing any validations. 

Post a Comment

 
Top