April 12, 2011

Struts 2 Basic Validation with validate() method

1) Create a login.jsp page in the WebContent folder of the web application. The struts tag <s:actionerror> will display any validation error messages in the input.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h4>Enter your Username and Password!</h4>
<s:actionerror />
<s:form action="doLogin">
     <s:textfield name="username" label="Username"/>
     <s:password name="password" label="Password"/>
     <s:submit/>
</s:form>
</body>
</html>

2) Add a Login.java action class in the src folder - the action class must extend the ActionSupport class in order to avail of the basic (programmatic) validation support of Struts2.

package info.icontraining.struts2;

import com.opensymphony.xwork2.*;

public class Login extends ActionSupport {

   public String execute() {
      return Action.SUCCESS;
   }
 
   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() {
  
      boolean flag = false;
  
      if (getUsername().length() == 0) {
         addFieldError("username", "Username is required");
         flag = true;
      }
  
      if (getPassword().length() == 0) {
         addFieldError("password", "Password is required");
         flag = true;
      }
  
      if (!flag) 
         if (!getUsername().equals("dinesh") && !getPassword().equals("dinesh")) {
            addActionError("Username and/or Password incorrect. Please try again!");
         }
      }
}

The validate() method within the action class contains the code to validate the incoming user data. If validation errors occur it invokes the addFieldError() or the addActionError() methods.

3) Configure the struts.xml for the 2 actions, as follows,

<action name="Login">
 <result>/login.jsp</result>
</action>
  
<action name="doLogin" class="info.icontraining.struts2.Login">
 <result>/welcome.jsp</result>
 <result name="input">/login.jsp</result>
</action>

4) Create the welcome.jsp page which will be displayed when the user data is successfully validated

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h1>Welcome!</h1>
<h3>You have successfully logged in.</h3>
</body>
</html>

5) Test the code example by typing the following URL in the browser

http://localhost:8080/WebAppName/Login.action

- Press Submit without typing anything in the text fields
- Press Submit after typing in any one of the text fields
- Press Submit after typing something in both the text fields
- Press Submit after typing 'dinesh' in both the text fields

No comments:

Post a Comment