package com.mystruts.struts.action;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages;
import com.mystruts.struts.SaveLogon; import com.mystruts.struts.form.LogonForm;
/** * MyEclipse Struts * Creation date: 02-27-2006 * * XDoclet definition: * @struts.action path="/logon" name="logonForm" input="/form/logon.jsp" scope="request" validate="true" * @struts.action-forward name="logon_sc" path="/success.jsp" */ public class LogonAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/** * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward */ //通过ActionForm Bean的验证后,Struts框架会自动调用execute方法 public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { // TODO Auto-generated method stub
//获得表单数据中的username String username = (String)((LogonForm)form).getUsername();
//获得表单数据中的userpassword String userpassword = (String)((LogonForm)form).getUserpassword();
//定义非法用户名 String badusername = "admin";
//定义非法密码 String baduserpassword = "123456";
//声明一个ActionMessages对象 ActionMessages errors = new ActionMessages();
if(badusername.equals(username)){
//如果用户名验证失败,将ActionMessage对象保存到ActionMessages对象errors中。logon.badusername.error对应消息资源文件中的logon.badusername.error的值。 errors.add("username",new ActionMessage("logon.badusername.error",badusername));
//保存ActionMessages对象errors到request范围。 saveErrors(request,errors);
//返回ActionForward对象,Struts框架会把请求转发到视图组件logon.jsp中,通过<html:errors/>来显示错误信息。 return(new ActionForward(mapping.getInput())); }
if(userpassword.equals(baduserpassword)){ errors.add("userpassword",new ActionMessage("logon.baduserpassword.error",baduserpassword)); saveErrors(request,errors); return(new ActionForward(mapping.getInput())); }
//定义模型组件SaveLogon SaveLogon sl = new SaveLogon(); sl.setUsername(username); sl.setUserpassword(userpassword); sl.saveToDb();
//控制器组件将SavaLogon对象保存到request范围,savalogon对应视图组件success.jsp中<login:present>标签的name以及<bear:write>标签的name request.setAttribute("savelogon",sl);
//清除request范围内的ActionForm Bean,因为后面的请求已经不需要了 request.removeAttribute(mapping.getAttribute());
//控制器组件将信息转交到视图组件logon.jsp,logon_sc对应struts-config.xml中的forward元素的name return(mapping.findForward("logon_sc")); }
}
|