Newsletter

Struts2 XML Validations Example, Declarative Validations In Struts

Struts » on Oct 25, 2011 { 8 Comments } By Sivateja

In struts 2 we have 3 types of validations

  • Programmatic validations [ manually ]
  • Declarative validations [ xml validations ]
  • Using Annotations

i already explained how we can do programmatic validations,  now will see how to do declarative validations, friends observe am going to use multiple validations rules for the field mail

Example

files required…

  • index.jsp
  • success.jsp
  • error.jsp
  • LogingEx.java [ in java4s package ]
  • LogingEx-validation.xml [ in java4s package, must be with that .class file]
  • web.xml [ in web-inf ]
  • struts.xml [ in web-inf/classes folder ]

Directory Structure

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
   <%@taglib uri="/struts-tags" prefix="s" %>
<html>

<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>
        <s:head />
</head>

<body>

<s:actionerror />
<s:form action="verify">

    <s:textfield name="uname" label="Enter Username" required="true"/><br>
    <s:textfield name="age" label="Enter Age" required="true"/><br>
    <s:textfield name="mail" label="Enter Email id" required="true"/><br>

    <s:submit value="Click" align="center" /> 

</s:form>
</body>
</html>

success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

Hello <s:property value="uname" /><br>
Your Age is:<s:property value="age" /><br>
Your mail:<s:property value="mail" />

error.jsp

This is error page

LogingEx.java

package java4s;
import com.opensymphony.xwork2.ActionSupport;
public class LogingEx extends ActionSupport{
    private static final long serialVersionUID = 1L;

    private String uname,mail;
    private int age;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String execute()
    {        

            return SUCCESS;

    }

}

LogingEx-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="uname">
    <field-validator type="requiredstring">
       <message>User Name is required.</message>
    </field-validator>
</field>

<field name="age">
   <field-validator type="int">
        <param name="min">15</param>
        <param name="max">25</param>
        <message>Age must be ${min} - ${max}</message>
   </field-validator>
</field>

<field name="mail">
    <field-validator type="requiredstring">
    <message>email required</message>
    </field-validator>

    <field-validator type="email">
    <message>enter valid email id</message>
    </field-validator>
</field>

</validators>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

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>
    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">
        <action name="verify" class="java4s.LogingEx">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
    </package>
</struts>

Output if enter wrong user/pass

Output

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

8 Responses to “Struts2 XML Validations Example, Declarative Validations In Struts”
  1. Arun says:

    Can we configure single validation.xml in struts 2 for all the action being used in our project?

  2. rishi says:

    Is there any possible to apply XML and programmatic validations in same form?……………….

  3. Nitya says:

    How to properly format the error message.Because the messages are displayed on top of the UI controlls?

  4. How to properly format the error message.Because the messages are displayed on top of the UI?

  5. Not able getting the error messages from from LoginEx-validation.xml

    where we need configure

  6. Chakravarthi says:

    Hi Java4s,

    in which situation we will go for declarative validations and in which situation we will go for programatic validaions can u plz clarify?

  7. Test+user says:

    if we have separate java bean class for form inputs instead having a getter and setter in action class then how we implement the declarative validation?

  8. RKRAJ says:

    Good for beginner

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.