Re-implementing the preceding example i.e, registration process example to use validation frame work. The additional code that has to be included into the respective files compared to the proceeding example are shown in bold letters, the following listing shows the Refistration.jsp

The additional code that has to be included into the respective files compared to the proceeding example are shown in bold letters, the following listing shows the Registration.jsp

Registration.jsp

<html><body>
<font color=”red” size=”5”>
<html:errors/><br/>
</font>
<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 demonstrate how to read the data from the DynaActionForm

RegisterAction.java

Package com.chandra.struts;
Import org.apache.struts.action.*;
Import javax.servelt.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.preparedStatement(“insert into 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.findForward(“success”);
}//try
Finally(con.close())
Return am.findForward(“fail”);
}//execute
}//class

The following listing shows the struts configuration that includes the dynamic validation form bean declaration and the validated plugin configuration.

Struts-config.xml

<struts-config>
 <form-beans>
   <form-bean name=”registerform” type=”org.apache.struts.validator.DynaValidatorForm”>
  <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-beans>
<action-mappings>
 <action path=”/register” type=”com.chandra.struts.RegisterAction” name=”registerform”
            Scope=”session” validate=”true” input=”/Registration.jsp”>
 <forward name=”success” path=”/Home.jsp”/>
<forward name=”fail” path=”/Registration.jsp”/>
</action>
</action-mappings>
<message-resources parameter=”ApplicationREsources”/>
<plug-in className=”org.apche.struts.validator.ValidatorPlugIn”>
 <set-property property=”pathnames” value=”/WEB-INF/validator-rules.xml,
                                                                                   /WEB-INF/validation.xml”/>
</plug-in>
</struts-config>

The following listing shows the validation.xml, which includes the validation rules for the registerform fields.

Validation.xml

<form-validation>
 <formset>
  <form name=”registerform”>
 <field property=”uname” depends=”required”>
  <arg0 key=”uname”/>
</field>
<field property =”pass” depends=”required,minilength,maxlength”>
<arg0 key=”pass”/>
<arg1 key=”4” resource=”false” name=”minilength”/>
<arg1 key=”${var.maxlength}” resource=”false” name=”maxlength”/>
<var>
<var-name>minilength</var-name>
<var-value>4</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>15</var-value>
</var>
</field>
<field property=”email” depends=”required.email”>
 <arg0 key=”email”/>
</field>
<field property=”address” depends=”required,email”>
<arg0 key=”address”/>
</field>
</form>
</formset>
</form-validation>

The following listing shows the ApplicationResources_en.properties file, this file contains the error messages that have to be presented in case of any errors.

ApplicationResources_en.properties

#ApplicationResources.properties
Uname=<b>User Name</b>
Pass=<b>Password</b>
Email=<b>Email ID</b>
Address=<b>Address</b>
#error keys(used by validator framework)
Errors.required=<li><i>{0}field cannot be empty</i></li></br>
Errors.minilength=<li><i>{0}can not be less than {1} characters</li><br/>
Errors.maxlength=<li><i>{0]can not be grater than{1}
Characters</i></li><br/>
Errors.email=<li><i>{0}is an invalid e-mail address</i></li><br/>

The following listing shows the web.xml file that is same as of the other examples that demonstrate up to now in this post

Web.xml

<web-app>
<servlet>
<servlet-name>as</servlet-name>
<servlet-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 RegisterAction.java and arrange the files and deployed into web server




Post a Comment

 
Top