DispatchAction和ForwardAction

Posted 流年如水~烟雨随风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DispatchAction和ForwardAction相关的知识,希望对你有一定的参考价值。

添加功能的步骤:做页面——编写DAO类中的方法——编写和配置action。

如果多个action 使用一个formbean,这种事儿多发生在统一模块中,就可以用一个Action集中处理多个操作,而不要创建多个Action,是代码结构更加简洁。

可以使用 DispatchAction,需要引入struts-extras-1.3.10.jar

这个jar在struts 的blank 示例项目中没有可以在lib中找到,将其导入,有了该jar包才能以下的类 org.apache.struts.action.DispatchAction

于是后台action代码可以放在一个Action中如下:

  1 package action;
  2 
  3 import javax.servlet.http.HttpServletRequest;
  4 import javax.servlet.http.HttpServletResponse;
  5 
  6 import org.apache.struts.action.ActionForm;
  7 import org.apache.struts.action.ActionForward;
  8 import org.apache.struts.action.ActionMapping;
  9 import org.apache.struts.actions.DispatchAction;
 10 
 11 import dao.UserDAO;
 12 import myuser.User;
 13 
 14 public class UserAction extends DispatchAction {
 15 
 16     /**
 17      * 添加用户
 18      * @param mapping
 19      * @param form
 20      * @param request
 21      * @param response
 22      * @return
 23      * @throws Exception
 24      */
 25     public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
 26             HttpServletResponse response) throws Exception {
 27         User user = (User)form;
 28         UserDAO dao = new UserDAO();
 29         dao.addUser(user);
 30         return mapping.findForward("list");//跳转到userlist.jsp页面
 31     }
 32     
 33     /**
 34      * 检查用户名是否存在
 35      * @param mapping
 36      * @param form
 37      * @param request
 38      * @param response
 39      * @return
 40      * @throws Exception
 41      */
 42     public ActionForward checkExists(ActionMapping mapping, ActionForm form, HttpServletRequest request,
 43             HttpServletResponse response) throws Exception {
 44         User user = (User)form;
 45         UserDAO dao = new UserDAO();
 46         String rel = dao.checkExists(user) ? "1" : "0";
 47         response.getWriter().write(rel);
 48         return null;
 49     }
 50     
 51     /**
 52      * 删除用户
 53      * @param mapping
 54      * @param form
 55      * @param request
 56      * @param response
 57      * @return
 58      * @throws Exception
 59      */
 60     public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
 61             HttpServletResponse response) throws Exception {
 62         User user = (User)form;
 63         UserDAO dao = new UserDAO();
 64         dao.deleteUser(user);
 65         return null;
 66     }
 67     
 68     /**
 69      * 获取用户信息
 70      * @param mapping
 71      * @param form
 72      * @param request
 73      * @param response
 74      * @return
 75      * @throws Exception
 76      */
 77     public ActionForward getUserInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request,
 78             HttpServletResponse response) throws Exception {
 79         User user = (User)form;
 80         UserDAO dao = new UserDAO();
 81         user = dao.getUserInfo(user);
 82         request.getSession().setAttribute("user",user);
 83         return mapping.findForward("updateUser");
 84     }
 85     
 86     /**
 87      * 修改用户
 88      * @param mapping
 89      * @param form
 90      * @param request
 91      * @param response
 92      * @return
 93      * @throws Exception
 94      */
 95     public ActionForward modify(ActionMapping mapping, ActionForm form, HttpServletRequest request,
 96             HttpServletResponse response) throws Exception {
 97         User user = (User)form;
 98         UserDAO dao = new UserDAO();
 99         dao.updateUser(user);
100         return mapping.findForward("list");
101     }
102 
103 }
View Code

struts-config文件的配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3  <!DOCTYPE struts-config PUBLIC
 4      "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
 5      "http://struts.apache.org/dtds/struts-config_1_3.dtd">
 6  <struts-config>
 7      <form-beans>
 8          <form-bean name="user" type="myuser.User"/>
 9      </form-beans>
10      
11      <global-forwards>
12          <forward name="list" path="/userlist.jsp"/>
13      </global-forwards>
14      
15      <action-mappings>
16         <action path="/user" type="action.UserAction" name="user" parameter="op">
17             <forward name="updateUser" path="/updateUser.jsp" redirect="true"/>
18         </action>
19         <action path="/main" type="org.apache.struts.actions.DispatchAction" parameter="/WEB-INF/page/main.jsp" />    
20      </action-mappings>
21  </struts-config>
View Code

访问的话就是譬如:user.do?op=add,注意:parameter参数配置的就是操作op,op的值就是方法名

通过DispatchAction,可以将多个使用同样formbean的action整合成一个action

org.apache.struts.action.ForwardAction 就是可以将jsp文件放置到 /WEB-INF下面,然后通过路径进行访问,配置如上,但是我访问main.do的时候出异常了,原因还没找到。

 代码链接:http://pan.baidu.com/s/1jHPe18q 提取码:ekyn

以上是关于DispatchAction和ForwardAction的主要内容,如果未能解决你的问题,请参考以下文章

echarts 常用API action 之 dispatchAction

echarts API action 之 dispatchAction

使用echarts在地图中使用dispatchAction

有关struts中DispatchAction的用法小结

使用struts中的DisPatchAction的时候需要用到的jar包

Struts1.3——DispatchActionDynamicForm和全局跳转