DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Struts 2 Validation Using XML File Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · · Tutorial
Like (1)
Save
Tweet
93.92K Views

Join the DZone community and get the full member experience.

Join For Free

In this example we will see how we can perform validations using XML validation file. The naming convention of the XML validation file should be ActionClass-Validation.xml. Here our Action Class name is "Login.java" and the XML validation file name is "Login-Validation.xml".

The Login-Validation.xml file contains the following code.

<!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>User Name is required.</message>
        </field-validator>
    </field>
    <field name="password">
        <field-validator type="requiredstring">
        	<message key="password.required" />
        </field-validator>
    </field>
</validators>

The field element contains the name of the form property that needs to be validated. The filed-validator element inside the field element contains the type of validation that needs to be performed.

Here you can either specify the error message directly using the message element or you can use the properties file to define all the errror messages and use the key attribute to specify the error key.

Note the properties file should also have the same name as the Action class.

The Login Action class contains the following code.

public class Login extends ActionSupport {

    private String userName;
    private String password;

    public Login() {
    }

    public String execute() {
        return SUCCESS;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

The login.jsp page contains the following code.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@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:form action="LoginAction">
        <s:textfield name="userName" label="User Name" />
        <s:password name="password" label="Password" />
        <s:submit value="Login" />
    </s:form>
</body>
</html>

The <s:head /> tag is used to include the required css and js file for the selected theme. By default the xhtml theme is used.

Execute the example and click the Login button without entering the user name and password the following page will be displayed.

Enter a user name and password and click the Login button the following success page will be displayed.

You can download the Struts 2 Validation using XML file example by clicking the download link.

Source :Download
Source + Lib :Download
XML

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Refactoring Java Application: Object-Oriented And Functional Approaches
  • Which JVM Version Is the Fastest?
  • Deployment of Low-Latency Solutions in the Cloud
  • Why I'm Choosing Pulumi Over Terraform

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo