WEB开发网
开发学院软件开发Java StrutsTestCase 简化开发过程 阅读

StrutsTestCase 简化开发过程

 2010-03-19 00:00:00 来源:WEB开发网   
核心提示: LoginActionForm.java 类创建 LoginActionForm.java 类,如清单 2 所示:清单 2. LoginActionForm.javapublicclassLoginActionFormextendsActionForm{publicActionErrorsval

LoginActionForm.java 类

创建 LoginActionForm.java 类,如清单 2 所示:

清单 2. LoginActionForm.java

public class LoginActionForm extends ActionForm { 
public ActionErrors validate( 
   ActionMapping mapping, 
   HttpServletRequest request) { 
   ActionErrors errors = new ActionErrors(); 
   if (userName == null || userName.length() == 0) 
      errors.add("userName", new ActionError("username.required")); 
   if (password == null || password.length() == 0) 
      errors.add("password", new ActionError("password.required")); 
   if( isUserDisabled(userName)) 
      errors.add("userName",new ActionError("user.disabled")); 
   return errors; 
} 
//Query USERDISABLED table to check if user account is disabled 
public boolean isUserDisabled(String userName) { 
   //SQL logic to check if user account is disabled 
} 
} 

在 validate() 方法中,需要检测用户是否输入了用户名和口令,因为这些字段是必需的。而且,还需要查询 USERDISABLED 表,确认用户的帐户没有被禁用。

LoginAction.java 类

接下来,要创建 LoginAction.java 类,如清单 3 所示:

清单 3. LoginAction.java 类

public class LoginAction extends Action { 
   public ActionForward execute( 
      ActionMapping mapping, 
      ActionForm form, 
      HttpServletRequest request, 
      HttpServletResponse response) 
      throws Exception { 
if (isValidUser(loginForm.getUserName(), loginForm.getPassword())) { 
         request.getSession().setAttribute( 
            "userName", 
            loginForm.getUserName()); 
         return mapping.findForward("success"); 
      } else { 
         ActionErrors errors = new ActionErrors(); 
         errors.add("userName", new ActionError("invalid.login")); 
         saveErrors(request, errors); 
         return new ActionForward(mapping.getInput()); 
      } 
   } 
//Query User Table to find out if userName and password combination is right. 
   public boolean isValidUser(String userName, String password) { 
    //SQL Logic to check if username password combination is right  
   } 
} 

上一页  1 2 3 4 5 6 7  下一页

Tags:StrutsTestCase 简化 开发

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接