In this example, you will see how to validate login through Ajax in struts2.
1-index.jsp
<html>ADS_TO_REPLACE_1 <head> <title>Ajax_Validation_Example</title> </head>ADS_TO_REPLACE_2 <body><h2>Ajax_Validation_Example</h2><hr/> <a href="loginForm.action">Go to loginForm</a> </body>ADS_TO_REPLACE_3 </html> |
2_ LoginActionForm.jsp
<%@taglib uri="/struts-tags" prefix="s"%> <%@taglib uri="/struts-dojo-tags" prefix="sx"%> <html>ADS_TO_REPLACE_4 <head><title>Ajax_Validation_Example</title><sx:head/></head> <body><h2>Ajax_Validation_Example</h2><hr/> <s:actionerror/>ADS_TO_REPLACE_5 <table border="1"><tr><td> <s:form action="loginValidation" > <tr><td>ADS_TO_REPLACE_6 <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>ADS_TO_REPLACE_7 <sx:submit validate="true"> </sx:submit> </s:form></td></tr></table> </body>ADS_TO_REPLACE_8 </html> |
package roseindia; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { ADS_TO_REPLACE_9 private String name; private int password; private String userEmail; ADS_TO_REPLACE_10 public String getName() { return name; } ADS_TO_REPLACE_11 public void setName(String name) { this.name = name; } ADS_TO_REPLACE_12 public int getPassword() { return password; } ADS_TO_REPLACE_13 public void setPassword(int password) { this.password = password; } ADS_TO_REPLACE_14 public String getUserEmail() { return userEmail; } ADS_TO_REPLACE_15 public void setUserEmail(String userEmail) { this.userEmail = userEmail; } ADS_TO_REPLACE_16 public String execute() throws Exception { System.out.println("Validating login"); if ((!getName().equals("bharat"))) {ADS_TO_REPLACE_17 addActionError("Invalid user name! Please try again!"); return ERROR; } else {ADS_TO_REPLACE_18 return SUCCESS; } } } |
4_ LoginActionForm-validation.xml
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" ADS_TO_REPLACE_19 "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="name">ADS_TO_REPLACE_20 <field-validator type="requiredstring"> <message>Please enter your name</message> </field-validator>ADS_TO_REPLACE_21 </field> <field name="password"> <field-validator type="int">ADS_TO_REPLACE_22 <param name="min">6</param> <param name="max">12</param> <message>Password must be in between minimum ${min} and maximum${max}.</message>ADS_TO_REPLACE_23 </field-validator> </field> <field name="userEmail">ADS_TO_REPLACE_24 <field-validator type="email"> <message>Please enter valid email id.</message> </field-validator>ADS_TO_REPLACE_25 </field> <field name="userEmail"> <field-validator type="requiredstring">ADS_TO_REPLACE_26 <message>Please enter email</message> </field-validator> </field>ADS_TO_REPLACE_27 </validators> |
5_ struts.xml
<struts> <constant name="struts.devMode" value="false" /> <package name="struts2" extends="struts-default">ADS_TO_REPLACE_28 <action name="loginForm"> <result>/LoginActionForm.jsp</result> </action>ADS_TO_REPLACE_29 <action name="loginValidation" class="roseindia.LoginAction"> <interceptor-ref name="jsonValidationWorkflowStack"/> <result name="input">/LoginActionForm.jsp</result>ADS_TO_REPLACE_30 <result name="error">/LoginActionForm.jsp</result> <result>/success.jsp</result> </action>ADS_TO_REPLACE_31 </package> </struts> |
6_ success.jsp
<%@taglib uri="/struts-tags" prefix="s" %> <html>ADS_TO_REPLACE_32 <head> <title>Login Success</title> </head>ADS_TO_REPLACE_33 <body> <h2>Ajax_Validation_Example</h2><hr/> <b> User Name : </b> <s:property value="name"/> <br/>ADS_TO_REPLACE_34 <b> Password : </b> <%=request.getParameter("password")%> <br/> <b>Email id : </b><s:property value="userEmail"/> </body>ADS_TO_REPLACE_35 </html> |
Output
ADS_TO_REPLACE_36
ADS_TO_REPLACE_37
ADS_TO_REPLACE_38
Advertisements
Ads
Ads