Here we will see how to write and implement the Struts 2.x Validation.
Create a Web Application project in Eclipse
1) Write login.jsp, login_success.jsp and login_failed.jsp under WebContent folder
2) Design the web.xml to point the the login.jsp
3) Develop the LoginAction.java , UserModel.java under src folder
4) Write the required validation properties in LoginAction.properties under src folder
5) struts.xml will the action mapping under src folder
The code for each file :
login.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<body>
<h2> Welcome to STruts2 validation demo</h2>
<struts2:form action="login">
<struts2:textfield name="userName" label="LoginName"/>
<struts2:password name="password" label="Password"/>
<struts2:submit value="Login"/>
</struts2:form>
</body>
</html>
login_success.jsp
<html>
<body>
<h2> user is successfully logged in</h2>
</body>
</html>
login_failed.jsp
<html>
<body>
<h2> UserName/password is invalid</h2>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Struts2Example2</display-name>
<filter>
<filter-name>Struts2Controller</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2Controller</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
LoginAction.java
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
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 void validate() {
System.out.println("valdate method");
System.out.println("userName == "+userName);
System.out.println("password == "+password);
if(userName == null || userName.length() == 0){
System.out.println("user name is empty");
addFieldError("userName", getText("myapp.struts2.userName.required"));
}
if(password == null || password.length() == 0){
System.out.println("password is empty");
addFieldError("password", "Password is mandatory");
}
}
public String execute() throws Exception {
System.out.println("execute method");
boolean result = UserModel.isUserNameAndPasswordExist(userName, password);
if(result){
return "success";
}else{
return "failure";
}
}
}
Create a Web Application project in Eclipse
1) Write login.jsp, login_success.jsp and login_failed.jsp under WebContent folder
2) Design the web.xml to point the the login.jsp
3) Develop the LoginAction.java , UserModel.java under src folder
4) Write the required validation properties in LoginAction.properties under src folder
5) struts.xml will the action mapping under src folder
The code for each file :
login.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<body>
<h2> Welcome to STruts2 validation demo</h2>
<struts2:form action="login">
<struts2:textfield name="userName" label="LoginName"/>
<struts2:password name="password" label="Password"/>
<struts2:submit value="Login"/>
</struts2:form>
</body>
</html>
login_success.jsp
<html>
<body>
<h2> user is successfully logged in</h2>
</body>
</html>
login_failed.jsp
<html>
<body>
<h2> UserName/password is invalid</h2>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Struts2Example2</display-name>
<filter>
<filter-name>Struts2Controller</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2Controller</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
LoginAction.java
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
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 void validate() {
System.out.println("valdate method");
System.out.println("userName == "+userName);
System.out.println("password == "+password);
if(userName == null || userName.length() == 0){
System.out.println("user name is empty");
addFieldError("userName", getText("myapp.struts2.userName.required"));
}
if(password == null || password.length() == 0){
System.out.println("password is empty");
addFieldError("password", "Password is mandatory");
}
}
public String execute() throws Exception {
System.out.println("execute method");
boolean result = UserModel.isUserNameAndPasswordExist(userName, password);
if(result){
return "success";
}else{
return "failure";
}
}
}
UserModel.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class UserModel {
private static Connection con;
static {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE", "user", "passwd");
}catch(Exception e){
e.printStackTrace();
}
}
public static boolean isUserNameAndPasswordExist (
String userName,String password){
boolean result = false;
try{
String sql = "select * from user where password = ? and login_name = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(2, userName);
ps.setString(1, password);
ResultSet rs = ps.executeQuery();
if(rs.next() && rs.getInt(1)> 0){
result = true;
}
}catch(SQLException e){
e.printStackTrace();
}
return result;
}
}
LoginAction.properties
myapp.struts2.userName.required=UserName is required
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" namespace="/" extends="struts-default">
<action name="login" class="LoginAction">
<result name="success">/login_success.jsp</result>
<result name="failure">/login_failed.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>