LoginAction extends Action

Action 类

 


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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 IllegalUserException;
import UserBiz;
import User;

/**
 * Struts的Action类的编写、配置及执行流程说明。
 * 编写Action: 1)继承于org.apache.struts.action.Action
 *                    2)覆盖execute方法
 *                    3)必须有缺省构造器
 * 实现execute方法:
 * 1)解析请求(参数、路径、头)
 * 2)创建模型(业务)对象,并使用该对象处理请求
 * 3)根据处理结果,返回代表下一个页面的ActionForward对象。
 *  配置: 在struts-config.xml的中加入:
 *
 *     
 *     
 *

 *

 *属性说明:
 * path:Action的访问路径,和

关联 在系统中是唯一的。
 * type:取值为Action的类的全名

 *
 * 属性说明:
 * name:自定义的名称,在mapping.findForward(String name)中用。
 * path:forward所代表的页面的路径
 * redirect:true|false(缺省),通知ActionServlet是否使用重定向的 方式跳转到下一个页面。
 * true:response.sendRedirect(forward.getPath());
 * false:getRequestDispatcher(forward.getPath()).forward(request, response)
 */
public class LoginAction extends Action {

    /*
     * 执行的入口,同业务/模型关联。
     *
     * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
     *      org.apache.struts.action.ActionForm,
     *      javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     *     
     * @param mapping 封装了配置文件中的信息。
     *        重要的方法findForward(String name)
     * @param form 封装了当前请求中参数信息的一个JavaBean,要求:
     *        1)继承于ActionForm
     *        2)提供同请求参数相同名称的属性
     *        3)运行时,系统会调用set方法把请求的参数赋给它相应的属性
     *        4)系统会将刚创建的form对象保存到request或session的属性中以便复用。
     *          范围 由scope属性指定,可以取session(默认)或request
     *          属性名 由attribute属性指定了属性名,默认为name=""
     * @return ActionForward 封装了配置文件中,当前的信息。
     *         ActionServlet通过Action返回的ActionForward对象来跳转到
     *         下一个页面。
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = null;
        // 获取登录表单中的数据
        LoginForm loginForm = (LoginForm) form;
        String userName = loginForm.getUserName();
        String password = loginForm.getPassword();
        // 创建模型(业务)对象
        UserBiz userBiz = new UserBiz();
        // 执行业务方法处理请求的数据
        User user = null;
        try {
            userBiz.find(userName, password);
        } catch (IllegalUserException e) {
            e.printStackTrace();
            throw e;
        }
        // 根据业务的执行结果跳转到下一个页面
        if (user != null) {
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            forward = mapping.findForward("success");
        } else {
            forward = mapping.findForward("fail");
        }
        return forward;
    }
}

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

form类 LoginForm

 

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

@SuppressWarnings("serial")
public class LoginForm extends ActionForm {
    private String userName;

    private String password;

    /**
     * 服务器端的校验方法
     * 该方法用来校验表单中的输入信息是否满足Action执行的要求。
     * 该方法是在系统调用setters之后,Action.execute之前执行。
     * 如果校验失败,即返回的ActionErrors中包含错误信息,
     * 系统会将返回的ActionErrors对象保存到当前请求的属性中。
     * Struts提供了标签:来读取请求中错误消息,并表现到页面中。
     * 系统会转到input所指定的页面。
     *
     * @param mapping ActionMapping对象,同execute方法参数一样。
     * @param request HttpServletRequest对象,同execute方法参数一样。
     *
     * @return ActionErrors 代表校验的结果,系统会根据它来决定下一步的操作。
     *         1)成功,执行Action.execute方法
     *         2)失败,回到输入界面重新输入信息
     *         3)成功、失败的涵义
     *         成功:返回值为null
     *         返回的ActionErrors的isEmpty方法为true 失败:返回的ActionErrors中包含出错信息。        
     */
    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (userName == null || userName.trim().length() == 0) {
            //errors.username为消息文件中消息的key
            //消息文件在struts-config.xml中注册
            ActionMessage message = new ActionMessage("errors.username");
           
            //参数username是ActionErrors存储message所有的key
            errors.add("username", message);
        }

        if (password == null || password.trim().length() == 0) {
            ActionMessage message = new ActionMessage("errors.password");
            errors.add("password", message);
        }

        return errors;
    }

    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;
    }
}

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

异常类 IllegalUserException

 

public class IllegalUserException extends Exception {

    public IllegalUserException() {       
    }

    public IllegalUserException(String arg0) {
        super(arg0);       
    }

    public IllegalUserException(Throwable arg0) {
        super(arg0);       
    }

    public IllegalUserException(String arg0, Throwable arg1) {
        super(arg0, arg1);       
    }
}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

struts-config.xml

 

                        name="loginForm" input="/core/login.jsp" scope="session" validate="true">
             

                                                                                                             path="/error.jsp" />     
             
             
     

     

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

ApplicationResources.properties

errors.illegaluser=you can not login as administrator.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

login.jsp

 

<%@page contentType="text/html;charset=gbk" %>
<%@taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>



  登录


 


   

欢迎使用本系统,请输入用户名和密码


   

   
   

    <%--
      form表单的action属性(链接的href)决定了处理表单中数据的服务器端Action类
      如何指定要调用的Action?
      应用的路径 + Action的路径 + ".do"
    --%>
   
       <%--
           是Struts框架提供一个标签,用来
           在JSP页面中动态的生成HTML中的标记
           通常只需指定其action属性,取值为struts-config.xml中的path属性。
           注意:一定要保证所指定的action使用了ActionForm
       --%>
   
     
       
       
     
     
       
       
     
   
用户名
          <%--
              是Struts框架提供的一个标签,用来生成
              HTML的控件
              property="":值是外层所指定的ActionForm的
                           属性名。
          --%>
         
         
       
密码
         
         
       

   

   
   

   


    <%--
      1)读取资源文件中errors.header消息并显示到页面中。
      2)读取validate方法返回的ActionErrors中包含的所有的消息并显示到页面中。
      3)读取资源文件中errors.footer消息并显示到页面中。
      总结:将格式放到资源文件的解决方案。
    --%>
   
   



   

        <%--
           循环处理的标签,即将validate方法返回的ActionErrors中包含的消息依次输出。
           1)允许在标签体内每次处理ActionErrors中一个消息。
           总结:将格式放在JSP页面中的解决方案
        --%>
       
          ${msg}
       

       

 


 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

fail.jsp

 

<%@page contentType="text/html;charset=gbk" %>



  失败


 


   

对不起,用户名不存在或密码不正确


   

   
返回
 


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

success.jsp

 

<%@page contentType="text/html;charset=gbk" %>
<%@taglib prefix="html" uri="http://struts.apache.org/tags-html"%>



  成功


 


   

登录成功


   

    welcome, ${loginForm.userName}
    退出
 




你可能感兴趣的:(struts1.2)