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" pageEncoding="UTF-8"%><! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib uri="/struts-tags" prefix="s"%>< html>< head>< meta http-equiv="Content-Type" content="text/html; charset=UTF-8">< title>Login Validation Example</title>< s:head /></ head>< body bgcolor="lightblue">< s:actionerror />< s:form action="Login"> <s:textfield name="userName" label="User Name" /><br> <s:password name="password" label="Password" /> <s:submit /></ s:form></ body></ html> |
2-LoginAction.java
|
package roseindia;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport { private static final long serialVersionUID = 525429611271529243L; private String userName; private String password; public String getUserName() { return userName;} public void setUserName(String userName) { this.userName = userName;} public String getPassword() { return password;} public void setPassword(String password) { this.password = password;} public String execute() throws Exception { return SUCCESS;} public void validate() { if (getUserName().length() == 0) {addFieldError( "userName", getText("username.required"));} else if (!getUserName().equals("Rose")) {addFieldError( "userName", getText("username.wrong"));} if (getPassword().length() == 0) {addFieldError( "password", getText("password.required"));} } } |
3-success.jsp
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd" >< html>< head>< meta http-equiv="Content-Type" content="text/html; charset=UTF-8">< title>Login Successful</title></ head>< body bgcolor="lightblue">< h1>Login Successful</h1></ body></ html> |
4 struts.xml
|
<? xml version="1.0" encoding="UTF-8"?><! DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >< struts> <package name="default" extends="struts-default"> <action name="Login" class="roseindia.LoginAction" > <result name="input">/Login.jsp</result> <result name="success">/success.jsp</result> </action> </package></ struts> |
5 LoginAction.properties
|
username.required = UserName is required.username.wrong = UserName is wrong.password.required = Password is required. |
Login.jsp

required.gif

wrong.gif

incomplete.gif

success.gif
