4:01 pm - Saturday February 25, 2017

Struts 2 Validation using properties file 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 properties file 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", getText("uname.vali"));
        }
        if (getPass().length() == 0) {
            addFieldError("pass", getText("pass.vali"));
        }
        else if (!getUname().equals("veera") && !getPass().equals("pass")) {
        	 this.addActionError(getText("error.vali"));
        }

    }

}

candidjava.properties

uname.vali = User Name is required.
pass.vali = Password is required.
error.vali = User Name or Password is incorrect.

struts.properties

struts.custom.i18n.resources=candidjava

struts.xml

</span></h2>
&nbsp;
<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

<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>
&nbsp;

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head/>
</head>
<body>
<h4><u>Struts2 Validation Using Properties 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_properties

BACK

Filed in: Struts 2 Tutorial

Comments are closed.