Reply to comment

Struts 2 Validation - Example Program

Tagged:  

Example program showing how to validate input in struts 2 using xml configuration file.

AddEntry.java

package phoneBook;
import java.sql.Date;
import com.opensymphony.xwork2.ActionSupport;

public class AddEntry extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private String name = null;
	private String address = null;
	private int age = 0;
	private Date birthDate = null;
	private String phoneNumber = null;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthDate() {
		return birthDate;
	}
	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String execute() throws Exception{
		System.out.println("Name\t\t:" +name);
		System.out.println("Address\t\t:" +address);
		System.out.println("Age\t\t:"+age);
		System.out.println("Birth Date\t:"+birthDate);
		System.out.println("Phone Number\t:" +phoneNumber);
		return SUCCESS;
	}
}

 

AddEntry.properties

invalid.fieldvalue.birthDate = Birthdate is invalid

 

AddEntry-validation.xml

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>

    <validator type="requiredstring">
        <param name="fieldName">name</param>
        <param name="trim">true</param>
        <message>Field ${fieldName} is required</message>
    </validator>
    
     <validator type="stringlength">
        <param name="fieldName">name</param>
        <param name="minLength">2</param>
        <param name="trim">true</param>
        <message>Field ${fieldName} is too short, minimum length is ${minLength}</message>
    </validator>
        
    <validator type="requiredstring">
        <param name="fieldName">address</param>
        <param name="trim">true</param>
        <message>Field ${fieldName} is required</message>
    </validator>

    <validator type="int" short-circuit="true">
        <param name="fieldName">age</param>
        <param name="min">18</param>
        <param name="max">60</param>
        <message>Field ${fieldName} should be between ${min} and ${max}</message>
    </validator>
    
    <validator type="regex">
        <param name="fieldName">phoneNumber</param>
        <param name="expression"><![CDATA[([0-9\s\+-]+)]]></param>
        <param name="trim">true</param>
        <message>Field ${fieldName} is in invalid format</message>
    </validator>
    
</validators>

 

struts.xml

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

<struts>

    <package name="default" extends="struts-default">
        <action name="AddEntry" class="phoneBook.AddEntry">
            <result>/details.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
    </package>
    
</struts>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
</web-app>

 

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>

<s:fielderror />

<s:form action="AddEntry" theme="simple">
<pre>
Name:            <input type="text" name="name" value="${name }"/>
Address:        <input type="text" name="address" value="${address }"/>
Age:            <input type="text" name="age" value="${age }" />
Birth Date:        <input type="text" name="birthDate" value="${birthDate}"/>
Phone Number:         <input type="text" name="phoneNumber" value="${phoneNumber }" />
        <input type="submit" />
</pre>
</s:form>

</body>
</html>

 

details.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

You entered the following details:
<pre>
Name:             ${name }
Address:         ${address }
Age:             ${age }
Birth Date:         <s:date name="birthDate" format="MM/dd/yyyy" />
Phone Number:         ${phoneNumber }

</pre>
</body>
</html>

 

jar files:

  • commons-logging-1.0.4.jar
  • freemarker-2.3.8.jar
  • ognl-2.6.11.jar
  • struts2-core-2.0.11.jar
  • xwork-2.0.4.jar

 

file heirarchy:

struts 2 validation

 

validation output:

struts 2 validation

Reply

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.