11:34 pm - Saturday January 21, 2017

Struts2 Validation 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

Struts2 Validation Example and Tutorial


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;

	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 execute()
	{
		return SUCCESS;
	}

	public void validate() {
        if (getUname().length() == 0) {
            addFieldError("uname", "User Name is required");
        }
        if (getPass().length() == 0) {
            addFieldError("pass", getText("password is required"));
        }
        else if (!getUname().equals("veera") && !getPass().equals("pass")) {
        	 this.addActionError(getText("Invalid User Name or Password"));
        }

    }

}

Struts.xml

<struts><include file="struts-default.xml"/>
<package name="a" extends="struts-default">
<action name="Validation" class="com.candidjava.controller.Validation">
<result name="success">/success.jsp</result><result name="input">/index.jsp</result></action></package></struts>

web.xml

</span></h2>
<web-app 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>

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head/>
</head>
<body>
<h4><u>Struts2 Validation Example</u></h4>
<s:actionerror/>
<s:form action="Validation">
	<s:textfield name="uname" label="Enter Username" /><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 Struts2_validation

BACK

Filed in: Struts 2 Tutorial

Comments are closed.