Struts Basic Validation

Here we explain how to implement basic validation in struts. The requirements is very simple we just make a registration form with three fields- name, password and phone no.
So our requirements are:
  • All field should be mandatory. 
  • Minimum length of password is 8-digits.
  • And minimum length of phone no is 10-digits.

My purpose is very clear, here I don't focus on validation logic but my focus is how to do basic validation in struts framework.
For the basic validation we have to override the validate() method in form bean class, the signature of validate() method as follow:


public ActionErrors validate(ActionMapping am,HttpServletRequest req)

So for the above requirements we have to write following files:

  • RegistrationForm.java
  • RegistrationAction.java
  • Application.properties
  • struts-config.xml
  • registration.jsp
  • home.jsp
  • web.xml
Note: In WEB-INF/lib folder all jar files should be there 
  • commons-beanutils.jar
  • commons-collections.jar
  • commons-digester.jar
  • commons-fileupload.jar (your choice for uploading file)
  • commons-lang.jar
  • commons-logging.jar
  • commons-validator.jar
  • jakarta-oro.jar
  • struts.jar
  • struts-legacy.jar
and Tld files should in WEB-INF folder :
  • struts-bean.tld
  • struts-html.tld
  • struts-logic.tld
  • struts-nested.tld
  • struts-template.tld
  • struts-tiles.tld

Now we write source code of all above mentioned files.

RegistrationForm.java

package com.javaforecast.struts1;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;

public class RegistrationForm extends ActionForm {
private String username;
private String password;
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
private String phone;

public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone;
}

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public ActionErrors validate(ActionMapping am,HttpServletRequest req){
    ActionErrors errors=new ActionErrors();
    if(username==null||username.length()==0){
         errors.add("username", new ActionError("errors.regForm.name.required"));
    }
        
    if(password==null||password.length()==0){
         errors.add("password", new ActionError("errors.regForm.password.required"));
    }
    else if(password.length()<8){
         errors.add("password", new ActionError("errors.regForm.password.length"));
    }
    if(phone==null||phone.length()==0){
         errors.add("phone", new ActionError("errors.regForm.phone.required"));
    }
    else if(phone.length()<10){
         errors.add("phone", new ActionError("errors.regForm.phone.length"));
    }
    return errors;
}
}

RegistrationAction.java

package com.javaforecast.struts1;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.*;

public class RegistrationAction extends Action {
   
public ActionForward execute(ActionMapping am,ActionForm af,HttpServletRequest req,HttpServletResponse res)throws Exception{
String result=null;
RegistrationForm lf=(RegistrationForm)af;
String name=lf.getUsername();
String password=lf.getPassword();
String phone=lf.getPhone();
    result="home";
    req.setAttribute("name", name);
    req.setAttribute("password", password);
    req.setAttribute("phone", phone);
return am.findForward(result);
}
}

Application.properties

errors.regForm.name.required=<font color=red size=3>Username is required</font>
errors.regForm.password.required=<font color=red size=3>password is required</font>
errors.regForm.password.length=<font color=red size=3>Min length is 8-digit</font>
errors.regForm.phone.required=<font color=red size=3>Phone is required</font>
errors.regForm.phone.length=<font color=red size=3>Min length is 10-digit</font>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
<form-beans>
<form-bean name="regForm" type="com.javaforecast.struts1.RegistrationForm" />
</form-beans>
<action-mappings>
<action path="/RegSubmit" 
      name="regForm"
  type="com.javaforecast.struts1.RegistrationAction" scope="request" 
  input="/registration.jsp">
<forward name="home" path="/home.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.javaforecast.struts1.Application" />
</struts-config>




 registration.jsp

<%@taglib uri="WEB-INF/struts-html.tld" prefix="html"%>
<html>
<body bgcolor="yellow" >

<h1>Java Forecast</h1>
<h2><font color=red size=5>Registration Form</font></h2>

<html:form action="/RegSubmit">
<table>

<tr>
<td>Name</td>
<td><html:text  property="username"/><html:errors property="username"/></td>

</tr>

<tr>
<td>Password</td>
<td><html:password property="password"/><html:errors property="password"/></td>

</tr>
<tr>
<td>Phone No</td>
<td><html:text property="phone"/><html:errors property="phone"/></td>

</tr>

<tr>
<td> <html:submit value ="Submit"/></td>
</tr>
</table>
</html:form>

</body>
</html>


home.jsp


<html>
<body bgcolor="yellow">
<h1> Welcome to Java Forecast 4u</h1>
<h1>Your is:Mr.${name }!</h1>
<h2>Your Email ID is:${password }</h2>
<h2>Your Phone No is :${phone }</h2>
</body>
</html>


<html>
<body bgcolor="yellow">
<h1> Welcome to Java Forecast 4u</h1>
<h1>Your is:Mr.${name }!</h1>
<h2>Your Email ID is:${password }</h2>
<h2>Your Phone No is :${phone }</h2>
</body>
</html>

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Lab1</display-name>
  <welcome-file-list>
   
    <welcome-file>registration.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>
  <init-param>
  <param-name>debug</param-name>
  <param-value>3</param-value>
  </init-param>
  <init-param>
  <param-name>detail</param-name>
  <param-value>3</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>

Now after successfully done above code you will get following results:


















Submitting form without filed values as 




















Now we submit form field value but password and phone no length is less than 8-digits and 10-digits corresponding.



















Finally we submit form field values as per our requirement and we get final result as 






0 comments :

Post a Comment