Home Struts Struts Struts2.2.1 Struts2 ajax validation example.

Ask Questions?

View Latest Questions


 
 

Struts2 ajax validation example.
Posted on: February 7, 2011 at 12:00 AM
Advertisement
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.

 1-index.jsp

<html>

<head>

<title>Ajax_Validation_Example</title>

</head>

<body><h2>Ajax_Validation_Example</h2><hr/>

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

</body>

</html>

2_ LoginActionForm.jsp

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

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

<html>

<head><title>Ajax_Validation_Example</title><sx:head/></head>

<body><h2>Ajax_Validation_Example</h2><hr/>

<s:actionerror/>

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

<s:form action="loginValidation" >

<tr><td>

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

</s:form></td></tr></table>

</body>

</html>

3_ LoginAction.java

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) {

this.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" 

"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>

<field name="name">

<field-validator type="requiredstring">

<message>Please enter your name</message>

</field-validator>

</field>

<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-validator>

</field>

<field name="userEmail">

<field-validator type="email">

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

</field-validator>

</field>

<field name="userEmail">

<field-validator type="requiredstring">

<message>Please enter email</message>

</field-validator>

</field>

</validators>

5_ struts.xml

<struts>

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

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

<action name="loginForm">

<result>/LoginActionForm.jsp</result>

</action>

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

<interceptor-ref name="jsonValidationWorkflowStack"/>

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

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

<result>/success.jsp</result>

</action>

</package>

</struts>

6_ success.jsp

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

<html>

<head>

<title>Login Success</title>

</head>

<body>

<h2>Ajax_Validation_Example</h2><hr/>

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

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

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

</body>

</html>

Output

Download Source Code

Related Tags for Struts2 ajax validation example. :


DMCA.com