Struts2 ajax validation example.
Posted on: February 7, 2011 at 12:00 AM
In this example, you will see how to validate login using ajax in struts.

Struts2 ajax validation example.

In this example, you will see how to validate login through Ajax in struts2.







<a href="loginForm.action">Go to loginForm</a>



2_ LoginActionForm.jsp

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

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





<table border="1"><tr><td>

<s:form action="loginValidation" >


<s:textfield label="Name" name="name"></s:textfield></td></tr>

<s:textfield name="password" label="password"></s:textfield>

<s:textfield name="userEmail" label="Email"></s:textfield>

<sx:submit validate="true"> </sx:submit>





package roseindia; 

import com.opensymphony.xwork2.ActionSupport; 

public class LoginAction extends ActionSupport { 

private String name;

private int password;

private String userEmail

public String getName() {

return name;

public void setName(String name) { = name;

public int getPassword() {

return password;

public void setPassword(int password) {

this.password = password;

public String getUserEmail() {

return userEmail;

public void setUserEmail(String userEmail) {

this.userEmail = userEmail;

public String execute() throws Exception {

System.out.println("Validating login");

if ((!getName().equals("bharat"))) {

addActionError("Invalid user name! Please try again!");

return ERROR;

} else {

return SUCCESS; }


4_ LoginActionForm-validation.xml

<!DOCTYPE validators PUBLIC 

"-//OpenSymphony Group//XWork Validator 1.0.2//EN" 



<field name="name">

<field-validator type="requiredstring">

<message>Please enter your name</message>



<field name="password">

<field-validator type="int">

<param name="min">6</param>

<param name="max">12</param>

<message>Password must be in between minimum ${min} and maximum${max}.</message>



<field name="userEmail">

<field-validator type="email">

<message>Please enter valid email id.</message>



<field name="userEmail">

<field-validator type="requiredstring">

<message>Please enter email</message>




5_ struts.xml


<constant name="struts.devMode" value="false" />

<package name="struts2" extends="struts-default">

<action name="loginForm">



<action name="loginValidation" class="roseindia.LoginAction">

<interceptor-ref name="jsonValidationWorkflowStack"/>

<result name="input">/LoginActionForm.jsp</result>

<result name="error">/LoginActionForm.jsp</result>





6_ success.jsp

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



<title>Login Success</title>




<b> User Name : </b> <s:property value="name"/> <br/>

<b> Password : </b> <%=request.getParameter("password")%> <br/>

<b>Email id : </b><s:property value="userEmail"/>




Download Source Code

Related Tags for Struts2 ajax validation example. :



Advertisement null