Struts(7)Struts的DispatchAction(分派Action)和全局跳转

1 为什么需要DispatchAction?

如果每个请求对应一个Action,就会造成Action过多,程序显得比较臃肿,因此程序需要减肥(可以把一类请求写到一个Action中处理),这样程序比较简洁,且利于维护和扩展。

2 DispatchAction开发

这里写图片描述
创建LoginAndLogoutAction 继承自DispatchAction
这里写图片描述
struts-config.xml添加parameter属性

<!-- parameter 用于区分用户不同的请求 -->
<action
  attribute="userForm"
  input="/WEB-INF/ok.jsp"
  name="userForm"
  parameter="flag"
  path="/loginAndLogout"
  scope="request"
  type="com.test.struts.action.LoginAndLogoutAction"
  validate="false"
  cancellable="true">
  <!-- 局部跳转 -->
  <forward name="goLogin" path="/WEB-INF/login.jsp" />
  <forward name="loginok" path="/WEB-INF/ok.jsp" />
</action>

②对应的LoginAndLogoutAction 文件

public class LoginAndLogoutAction extends DispatchAction {

    // 使用分派action,往往需要自己重新命名函数
    // 该函数用于响应登录请求
    public ActionForward userLogin(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        UserForm userForm = (UserForm) form;// TODO Auto-generated method stub

        if("123".equals(userForm.getPassword())) {
            return mapping.findForward("loginok");
        } else {
            return mapping.findForward("goLogin");
        }
    }
    // 该函数用于响应注销请求
    public ActionForward userLogout(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
        //清空session
        request.getSession().invalidate();
        return mapping.findForward("goerr"); // 全局跳转
    }
    // 该函数用于响应注销请求
    public ActionForward userLogout2(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
        //清空session
        System.out.println("logout2");
        request.getSession().invalidate();
        return mapping.findForward("goLogin");
    }
}

③跳转的URL格式
/dispatchtest/loginAndLogout.do?flag=userLogin

<form action="/dispatchtest/loginAndLogout.do?flag=userLogin" method="post">
    u:<input type="text" name="name"><br>
    p:<input type="password" name="password"><br>
    <input type="submit" value="login">
</form>

3 全局跳转和局部跳转

这里写图片描述
全局跳转:指的是所有的action都可以跳转到该页面;

<global-forwards >
  <forward name="goerr" path="/WEB-INF/err.jsp" />
</global-forwards>

局部跳转:指的是只有本action可以跳转到该页面。

<action
  attribute="userForm"
  input="/WEB-INF/ok.jsp"
  name="userForm"
  parameter="flag"
  path="/loginAndLogout"
  scope="request"
  type="com.test.struts.action.LoginAndLogoutAction"
  validate="false"
  cancellable="true">
  <!-- 局部跳转 -->
  <forward name="goLogin" path="/WEB-INF/login.jsp" />
  <forward name="loginok" path="/WEB-INF/ok.jsp" />
</action>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ADreamClusive

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值