Write A Login ActionForm

You had made a struts Input JSP View in previous section. To gather data from struts Input View Struts Frameworks provides org.apache.struts.action.ActionForm class.
With by use this class we have to create a subclass which used to gather all data coming from input view.
 Here In this subclass, We can validate data input form (we will discuss it later) and also define reset method.
The following code is appropriate subclass of ActionForm of Input JSP View (Which we had discuss in our previous topic).In this first we have create package then import all required resources(packages ). Make a subclass with the name LoginForm.Declare all variables (variables name must be same as property name as in Input JSP View). Create getter/setter method and finally create reset method .In reset method set all variables null. Configure ActionForm in struts-config.xml file.Like Here name attribute value is lookup value through which action controller lookup the appropriate subclass of ActionForm.type attribute is full qualified name of subclass of ActionForm. We can configure any number of ActionForm in struts-config.xml.
SourceCode:


/*** Sava as a LoginForm.java* */
package com.wisedeveloper;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LoginForm extends org.apache.struts.action.ActionForm{         
          /** This is First ActionForm Subclass          
            * This subclass is used to gather the information entered by use     
            * Here We are Gathering user name and password entered by user on WISEDEVELOPERFirstInputView.jsp file             */          
        private static final long serialVersionUID = 11112; 
        private String username,password;
        public String getPassword() {     
               return password;    }
               public void setPassword(String password) {     
               this.password = password;   }                  
        public String getUsername() {                   
        return username;           }            
        public void setUsername(String username) {            
               this.username = username;    }            
        public void reset(ActionMapping mapping, HttpServletRequest request) {     
        name = null;     
        password = null;    }   
        public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {       
                       ActionErrors errors = new ActionErrors();
               if (username == null || username.length() < 1) {                
               errors.add("username", new ActionMessage("error.username.required"));      
               }            
               else if (password == null || password.length() < 1) {                        
               errors.add("password", new ActionMessage("error.password.required"));      
               }                                           
               return errors;                                                       
}
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More