3:11 pm - Tuesday November 23, 2534

Struts 2 Validation using xml configuration Example and Tutorial

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Struts 2 Validation using xml configuration Example and Tutorial

Validation-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="pass">
<field-validator type="requiredstring">
<message>Password is required.</message>
</field-validator>
</field>

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

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

Validation.java

package com.candidjava.controller;

import com.opensymphony.xwork2.ActionSupport;

public class Validation extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private String uname;
	private String pass;
	private String mail;

	public String getUname() {
		return uname;
	}

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

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getMail() {
		return mail;
	}

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

	public String execute() {
		return SUCCESS;
	}
}

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="Validation">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>

index.jsp

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<s:head />
</head>
<body>
	<h4>
		<u>Struts2 Validation Using Xml Example</u>
	</h4>
	<s:form action="Validation">
		<s:textfield name="uname" label="Enter Username" />
		<br>
		<s:textfield name="mail" label="Enter E-mail" />
		<br>
		<s:textfield name="pass" label="Enter Password" />
		<br>
		<s:submit value="Login" align="center" />
	</s:form>
</body>
</html>

success.jsp


<%@ taglib prefix="s" uri="/struts-tags"%>
Welcome
<b><s:property value="uname" />,</b>
<br />
You have been successfully Logged in....

Output Screenshots:

                                   Download  Sourcecode

BACK

Filed in: Z-A All Java Codes

Comments are closed.