Struts Java Framework4 RegForm Using Validators

18.Part-15


index.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<h1>REG FORM</h1>

<html:form  action="reg" onsubmit="return validateRF(this);">
<html:javascript  formName="RF">

<pre>
ID:  <html:text  property="id"/><html:errors property="id"/>
NAME: <html:text  property="name"/><html:errors property="name"/>
EMAIL: <html:text  property="email"/><html:errors property="email"/>
SALARY: <html:text  property="salary"/><html:errors property="salary"/>
CREDITCARD: <html:text  property="creditcard"/><html:errors property="creditcard"/>
PHONE: <html:text  property="phone"/><html:errors property="phone"/>
<html:submit value="Register"/>

</pre></html:form>


one validator-rules.xml file create one validation xml file called EmpRegForm.xml



<!DOCTYPE form-validation PUBLIC

"-//Apache Software Foundation//DTD Commons Valida
""http://jakarta.apache.org/commons/dtds/validator_
<form-validation>
<!--common validations for locale-->
<formset>
<form name ="RF">
<field property="id"  depends="intRange">
<arg key="id" resource="false"/>
<arg key="${var:min}" resource="false"/>
<arg key="${var:max}" resource="false"/>
<var>
<var-name>min</var-name>
<var-val>111</var-val></var>

<var>
<var-name>max</var-name>
<var-val>999</var-val>
</var>
</field>
<field property="name"  depends="required">
<arg key="name" resource="false"/>
</field>
<field property="email"  depends="required">
<arg key="email" resource="false"/>
</field>
<field property="creditcard"  depends="required">
<arg key="creditcard" resource="false"/>
</field>
</form>
</formset>
</form-validation>



We create one error mesg in property file Messages.property



















<!--indian mobile validation and salary validation-->
<formset languages="en"  country="IN">
<form name="RF">
<field name="salary" depends="floatRange">
<arg key="salary" resource="false"/>
<arg key="${var:min}" resource="false"/>
<arg key="${var:max}" resource="false"/>
<var>
<var-name>min</var-name>
<var-val>10000.5</var-val></var>

<var>

<var-name>max</var-name>
<var-val>20000.5</var-val>
</var>


</field>

<field property="phone" depends="mask">
<arg key="phone" resource="false"/>
<var>
<var-name>mask</var-name>
<var-val>^[7-9]\d{9}$</var-val>
</var>

</field>


</form>

</formset>


<formset languages="en"  country="US">

<form name="RF">
<field name="salary" depends="floatRange">
<arg key="salary" resource="false"/>
<arg key="${var:min}" resource="false"/>
<arg key="${var:max}" resource="false"/>
<var>
<var-name>min</var-name>
<var-val>100000.5</var-val></var>

<var>

<var-name>max</var-name>
<var-val>200000.5</var-val>
</var>


</field>

<field property="phone" depends="mask">
<arg key="phone" resource="false"/>
<var>
<var-name>mask</var-name>
<var-val>^[2-4]\d{9}$</var-val>
</var>

</field>


</form>


</formset>









Create one bean class



package beans;
import org.apache.struts.validator.ValidatorForm;

public class RegForm extends ValidatorForm
{

private int id;
private long phone,creditcard;
private float salary;
private String name,email;


}



Generate Getters and Setters





<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1 
"http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

<form-beans>
<form-bean name="RF" type="beans.RegForm"/></form-bean>

</form-beans>

<!--form to validator-->
<action-mappings>
<action path="/reg" name="RF" validate="true" scope="session" input="/input.jsp"/></action>

<message-resources parameter="resources/Messages"/>

<plug-in className="org.apache.struts.validator.ValidatorPlugin">
<set-property property="pathnames" value="/org/apache/struts/validator/validator-rules.xml,/WEB-INF/EmpRegForm.xml"/>

</plug-in>
</action-mappings>

</struts-config>





web.xml


<?xml version="1.0"  encoding="UTF-8"?>
<web-app  xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta"

<display-name>RegForm_VF</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


<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>