In this example, We will discuss about the Login validation using struts2.2.1.
1-Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"ADS_TO_REPLACE_1 pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">ADS_TO_REPLACE_2 <%@taglib uri="/struts-tags" prefix="s"%> <html> <head>ADS_TO_REPLACE_3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login Validation Example</title> <s:head />ADS_TO_REPLACE_4 </head> <body bgcolor="lightblue"> <s:actionerror />ADS_TO_REPLACE_5 <s:form action="Login"> <s:textfield name="userName" label="User Name" /><br> <s:password name="password" label="Password" />ADS_TO_REPLACE_6 <s:submit /> </s:form> </body>ADS_TO_REPLACE_7 </html> |
2-LoginAction.java
package roseindia; import com.opensymphony.xwork2.ActionSupport;ADS_TO_REPLACE_8 public class LoginAction extends ActionSupport { private static final long serialVersionUID = 525429611271529243L; private String userName;ADS_TO_REPLACE_9 private String password; public String getUserName() { return userName;ADS_TO_REPLACE_10 } public void setUserName(String userName) { this.userName = userName;ADS_TO_REPLACE_11 } public String getPassword() { return password;ADS_TO_REPLACE_12 } public void setPassword(String password) { this.password = password;ADS_TO_REPLACE_13 } public String execute() throws Exception { return SUCCESS;ADS_TO_REPLACE_14 } public void validate() { if (getUserName().length() == 0) {ADS_TO_REPLACE_15 addFieldError("userName", getText("username.required")); } else if (!getUserName().equals("Rose")) { addFieldError("userName", getText("username.wrong"));ADS_TO_REPLACE_16 } if (getPassword().length() == 0) { addFieldError("password", getText("password.required"));ADS_TO_REPLACE_17 } } } |
3-success.jspADS_TO_REPLACE_18
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"ADS_TO_REPLACE_19 "http://www.w3.org/TR/html4/loose.dtd"> <html> <head>ADS_TO_REPLACE_20 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login Successful</title> </head>ADS_TO_REPLACE_21 <body bgcolor="lightblue"> <h1>Login Successful</h1> </body>ADS_TO_REPLACE_22 </html> |
4 struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLICADS_TO_REPLACE_23 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>ADS_TO_REPLACE_24 <package name="default" extends="struts-default"> <action name="Login" class="roseindia.LoginAction" > <result name="input">/Login.jsp</result>ADS_TO_REPLACE_25 <result name="success">/success.jsp</result> </action> </package>ADS_TO_REPLACE_26 </struts> |
5 LoginAction.properties
username.required = UserName is required. username.wrong = UserName is wrong.ADS_TO_REPLACE_27 password.required = Password is required. |
Login.jsp
ADS_TO_REPLACE_28
required.gif
wrong.gifADS_TO_REPLACE_29
incomplete.gif
ADS_TO_REPLACE_30
success.gif
Download Select Source CodeADS_TO_REPLACE_31
Advertisements
Ads
Ads