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的<action-mappings>中加入:
 * <action path="/core/login" type="LoginAction">
 *      <forward name="success" path="/core/success.jsp" redirect="true"/>
 *      <forward name="fail" path="/core/fail.jsp" />
 * </action>
 *

 *<action>属性说明:
 * path:Action的访问路径,和<form action=""> <a href="" />关联 在系统中是唯一的。
 * type:取值为Action的类的全名

 *
 * <forward>属性说明:
 * 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 封装了配置文件中<action></action>的信息。
     *        重要的方法findForward(String name)
     * @param form 封装了当前请求中参数信息的一个JavaBean,要求:
     *        1)继承于ActionForm
     *        2)提供同请求参数相同名称的属性
     *        3)运行时,系统会调用set方法把请求的参数赋给它相应的属性
     *        4)系统会将刚创建的form对象保存到request或session的属性中以便复用。
     *          范围 由<action scope="">scope属性指定,可以取session(默认)或request
     *          属性名 由<action attribute="">attribute属性指定了属性名,默认为name=""
     * @return ActionForward 封装了配置文件中,当前<action>的<forward>的信息。
     *         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提供了标签:<html:errors />来读取请求中错误消息,并表现到页面中。
     * 系统会转到<action input="" />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

 

      <action path="/core/login" type="LoginAction"
                  name="loginForm" input="/core/login.jsp" scope="session" validate="true">
              <exception key="errors.illegaluser" type="IllegalUserException"

                                                                                                             path="/error.jsp" />     
              <forward name="success" path="/core/success.jsp" redirect="true"/>
              <forward name="fail" path="/core/fail.jsp" />
      </action>

      <message-resources parameter="ApplicationResources" />

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

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"%>

<html>
<head>
  <title>登录</title>
</head>
<body>
  <center>
    <h3>欢迎使用本系统,请输入用户名和密码</h3>
    <hr>
    <bean:message key="errors.username" />
    <hr>
    <%--
      form表单的action属性(链接的href)决定了处理表单中数据的服务器端Action类
      如何指定要调用的Action?
      应用的路径 + Action的路径 + ".do"
    --%>
    <html:form action="/core/login">
       <%--
           <html:form></html:form>是Struts框架提供一个标签,用来
           在JSP页面中动态的生成HTML中的<form></form>标记
           通常只需指定其action属性,取值为struts-config.xml中<action>的path属性。
           注意:一定要保证所指定的action使用了ActionForm
       --%>
    <table width="300" border="1" align="center">
      <tr>
        <td nowrap>用户名</td>
        <td nowrap>
          <%--
              <html:text />是Struts框架提供的一个标签,用来生成
              HTML的<input type="text">控件
              property="":值是外层<html:form>所指定的ActionForm的
                           属性名。
          --%>
          <html:text property="userName" />
          <html:errors property="username"/>
        </td>
      </tr>
      <tr>
        <td nowrap>密码</td>
        <td nowrap>
          <html:password property="password" />
          <html:errors property="password"/>
        </td>
      </tr>
    </table>
    <br>
    <input type="submit" value="登录">
    </html:form>
    <p>
    <%--
      1)读取资源文件中errors.header消息并显示到页面中。
      2)读取validate方法返回的ActionErrors中包含的所有的消息并显示到页面中。
      3)读取资源文件中errors.footer消息并显示到页面中。
      总结:将格式放到资源文件的解决方案。
    --%>
    <html:errors />
    <hr>
    <ul>
    <%--
       循环处理的标签,即将validate方法返回的ActionErrors中包含的消息依次输出。
       1)允许在标签体内每次处理ActionErrors中一个消息。
       总结:将格式放在JSP页面中的解决方案
    --%>
    <html:messages id="msg" >
      ${msg}
    </html:messages>
    </ul>
  </center>
</body>
</html>

 

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

fail.jsp

 

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

<html>
<head>
  <title>失败</title>
</head>
<body>
  <center>
    <h3>对不起,用户名不存在或密码不正确</h3>
    <hr>
    <a href="javascript:history.back()">返回</a>
  </center>
</body>
</html>

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

success.jsp

 

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

<html>
<head>
  <title>成功</title>
</head>
<body>
  <center>
    <h3>登录成功</h3>
    <hr>
    welcome, ${loginForm.userName}
    <html:link action="/core/logout">退出</html:link>
  </center>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值