March 7, 2011

Struts 1.x - reset() and validate() methods in ActionForm

To setup the Struts Framework in your J2EE web application, visit the following post - http://www.javaissues.com/2011/03/struts-1x-hello-world-example.html

Create login.jsp

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<body>
   <html:errors />
   <form action="login.do" method="post">
      <input type="text" name="username" /> <br/>
      <input type="password" name="password" /> <br/>
      <input type="submit" value="Submit" />
   </form>
</body>
</html>


Create LoginForm.java

package info.icontraining.struts;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;

public class LoginForm extends ActionForm {

   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 reset(ActionMapping mapping, HttpServletRequest request) {
      username = null;
      password = null;
   }
 
   public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
      ActionErrors errors = new ActionErrors();
  
      if ((username == null) || (username.length() <= 0) ) {
          errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.username"));
      }
      if ((password == null) || (password.length() < 8)) {
          errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.password"));
      }
      return errors;
   }
}


Create LoginAction.java

package info.icontraining.struts;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.apache.struts.action.*;

public class LoginAction extends Action {

   public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
  
      String username = ((LoginForm)form).getUsername();
      String password = ((LoginForm)form).getPassword();
  
      if (username.equals("abc") && password.equals("def")) {
          return mapping.findForward("success");
      } else {
          return mapping.findForward("failure");
      }  
   }
}


Create ApplicationResources.properties and place it in the src folder (in Eclipse) or in the default package

error.username=Username is required<br/>
error.password=Password must contain minimum 8 characters<br/>


Add the following configuration to struts-config.xml

<struts-config>
...
   <form-bean name="loginForm" type="info.icontraining.struts.LoginForm" />
   ...

   <action path="/login" type="info.icontraining.struts.LoginAction" name="loginForm" validate="true" input="/login.jsp"> 
      <forward name="success" path="/loginSuccess.jsp" />
      <forward name="failure" path="/loginFailure.jsp" /> 
   </action>

   ...
   <message-resources parameter="ApplicationResources" />
...
</struts-config>


Visit the following URL in the browser,

http://localhost:8080/WebAppName/login.jsp

No comments:

Post a Comment