Struts2 – Validation Example, Struts 2 Validation

 

Struts2 Validation Example
Struts2 validation implement by xml file in struts 2. Validation is more easier to implement in struts2. We can define our rules of validation in xml file and use in jsp field. Struts2 provides more options of validation like email validation, integer range validation, form validation field, expression validation, regex validation, required validation, requiredstring validation, stringlength validation and etc.

Register jsp struts 2 application form can be validated with number of options which are shown in example.

Example will explain how to use validation in struts 2.

1. register.jsp registration form jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Validation Struts page</title>
    <s:head/>
</head>

<body>
<s:form action="Register">
    <s:textfield key="username"/>
    <s:password key="password" />
    <s:textfield key="email" />
    <s:select headerKey="" headerValue="Select Gender"
     key="gender" list="#{'M':'Male','F':'Female'}" />
    <s:textfield key="postalcode" /> 
    <s:submit/>
</s:form>
</body>
</html>

2. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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>Struts Blank</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

3. register.xml

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

<struts>

    <package name="register" namespace="/" extends="struts-default">

        <action name="Register" class="com.struts2.RegisterAction">
            <result name="input">/register.jsp</result>
            <result type="redirectAction">register.jsp</result>
        </action>

        <!-- Add actions here -->
    </package>
</struts>

4. struts.xml

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

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <include file="register.xml"/>
		
    <!-- Add packages here -->

</struts>

5. RegisterAction.java

package com.struts2;

public class RegisterAction extends MyRegisterSupport{
    
    private static final long serialVersionUID = 1L;

    public String execute() throws Exception {
        return SUCCESS;
    }
}

6. MyRegisterSupport.java

package com.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class MyRegisterSupport extends ActionSupport{

    private static final long serialVersionUID = 1L;
    
    private String username;
    private String password;
    private String email;
    private String gender;
    private String postalcode;
    
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getPostalcode() {
        return postalcode;
    }
    public void setPostalcode(String postalcode) {
        this.postalcode = postalcode;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
}

7. RegisterAction-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>
    <field name="username">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
    </field>
    <field name="password">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">6</param>
            <param name="trim">true</param>
            <message key="requiredpassword"/>
        </field-validator>
    </field>
    <field name="email">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
        <field-validator type="email">
            <message key="requiredemail"/>
        </field-validator>
    </field>
    <field name="gender">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
    </field>
    <field name="postalcode">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
        <field-validator type="regex">
            <param name="expression"><![CDATA[^d*$]]></param>
            <message key="requiredinteger"/>
          </field-validator>
    </field>
</validators>

8. package.properties

requiredstring = ${getText(fieldName)} is required.
requiredemail = ${getText(fieldName)} is not in correct format.
requiredpassword = ${getText(fieldName)} should be more than 6 character.
requiredinteger = ${getText(fieldName)} should be numeric.
password = Password
username = User Name
email = Email Address
gender = Gender
postalcode = Postal Code

Validation failed

empty field validation

Password field length check

Integer field validation check struts2

Email field validation

download source code of struts2 validation

Tags:

 

3 Responses to “Struts2 – Validation Example, Struts 2 Validation”

  1. Neela says:

    good:)

  2. Syeatha says:

    SORRY SIR,YOU DONE ONE MISTAKE.
    GIVE VALIDATION TO ‘MYREGISTERSUPPORT’ ACTION CLASS