struts控制器DispatchAction

上篇演示了struts框架的由来,从而体现struts框架优点。Struts中的表单处理器为ActionForm,而struts中的控制器主要是Action,以及DispatchAction控制器等。

Action

在struts中,所有的用户都会经过ActionServlet的处理,而实际的工作是交给Action对象来处理的,ActionServlet可以从配置文件中创建ActionMapping对象,从ActionMapping对象中找到对应使用的Action,然后将用户请求转交给Action。

对Struts一个ActionMapping只能生成一个Action对象,当用户发起请求的时候会检查所需要的Action对象是否存在,如果不存在就会生成一个Action对象,在以后的处理过程中使用这个对象。

当我们使用Action的时候需要继承arg.apache.struts.action.Action这个类,在子类中加入所需要的业务逻辑处理,这些子类会返回ActionForward对象,ActionServlet接受这个对象,把页面转发到指定页面,从而把用户请求的结果发送到对应的页面。我们在struts-config.xml中进行配置。配置的主要属性如下:

(1)  path属性:访问Action的URL地址,当用户请求路径和URL匹配时,ActionServlet会把这个用户请求发送给Action处理。

(2)  type属性:指定处理请求的Action对应的类,需要写类文件的包路径。

(3)  name属性:指定我们的Action用到的ActionForm名字,这个ActionForm必须是在<form-beans>中定义过的。

(4)  scope属性:指定ActionForm的使用范围,缺省值为session范围。

(5)  input属性:指定表单验证出错的时候转向页面。

(6)  validate属性:指明是否自动调用ActionForm中的validate方法对表单进行验证。

配置示例如下代码。

  1. <struts-config>  
  2.      <form-beans>  
  3.             <form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm" />  
  4.      </form-beans>  
  5.       
  6.      <action-mappings>  
  7.             <action path="/login"  
  8.                           type="com.bjpowernode.struts.LoginAction"  
  9.                           name="loginForm"  
  10.                           scope="request"  
  11.                           >  
  12.                           <forward name="success" path="/login_success.jsp"/>  
  13.                           <forward name="error" path="/login_error.jsp"/>  
  14.             </action>  
  15.      </action-mappings>  
  16.       
  17. </struts-config>  


问题

当我们完成用户增删改查操作时采用struts框架,我们需要为增删改查建立四个不同的Action,如果有更多的增删改查操作,比如对物料增删改查也需要建立四个Action,这样造成了大量的Action。

问题的解决

在struts中的Action类中,只提供了一个execute()方法,一个用户请求URL只能对应一个servlet,在struts中提供了另一个控制器类org.apache.struts.actions.DispatchAction,这个类可以经完成相关业务逻辑所需方法几种在一个DispatchAction类中,我们继承DispatchAction类后不重写execute()方法,而是编写自己的方法,在不同的方法中处理不同的动作。删除用户增删改查对应的Action,建立UserAction(如图5.1)。


图5.1

界面中调用代码如下所示。

  1. <body>  
  2. <a href="user/user_maint.do?command=list"title="请点击访问用户管理系统">用户管理系统</a>  
  3. </body>  


其中list对应着UserAction中的list方法,传递的字符串与UserAction中的方法名相同。

UserAction中的代码如下所示:

  1. packagecom.bjpowernode.drp.web.actions;  
  2.    
  3. importjava.util.Date;  
  4. importjava.util.List;  
  5.    
  6. importjavax.servlet.http.HttpServletRequest;  
  7. importjavax.servlet.http.HttpServletResponse;  
  8.    
  9. importorg.apache.commons.beanutils.BeanUtils;  
  10. importorg.apache.struts.action.ActionForm;  
  11. importorg.apache.struts.action.ActionForward;  
  12. importorg.apache.struts.action.ActionMapping;  
  13. importorg.apache.struts.actions.DispatchAction;  
  14.    
  15. importcom.bjpowernode.drp.manager.UserManager;  
  16. importcom.bjpowernode.drp.model.User;  
  17. importcom.bjpowernode.drp.web.forms.UserActionForm;  
  18.    
  19. public classUserAction extends DispatchAction {  
  20.    
  21.       
  22.    
  23.    
  24.      protected ActionForward list(ActionMapping mapping, ActionForm form,  
  25.                    HttpServletRequestrequest, HttpServletResponse response)  
  26.                    throwsException {  
  27.             //调用业务逻辑操作  
  28.             List userList = UserManager.getInstance().findAllUserList();  
  29.             request.setAttribute("userlist",userList);  
  30.              
  31.             returnmapping.findForward("list_success");  
  32.      }  
  33.    
  34.       
  35.      /** 
  36.       * 用户删除 
  37.       * @param mapping 
  38.       * @param form 
  39.       * @param request 
  40.       * @param response 
  41.       * @return 
  42.       * @throws Exception 
  43.       */  
  44.      public ActionForward del(ActionMapping mapping, ActionForm form,  
  45.                    HttpServletRequestrequest, HttpServletResponse response)  
  46.                    throws Exception {  
  47.             //获取从页面表单中提交过来的值  
  48.             UserActionForm uaf = (UserActionForm)form;  
  49.              
  50.             //取得需要删除的userId的集合  
  51.             String[] userIdList = uaf.getSelectFlag();  
  52.    
  53.             //调用业务逻辑操作  
  54.             UserManager.getInstance().deleteUsers(userIdList);  
  55.             return mapping.findForward("del_success");  
  56.      }  
  57.       
  58.      /** 
  59.       * 用户添加 
  60.       * @param mapping 
  61.       * @param form 
  62.       * @param request 
  63.       * @param response 
  64.       * @return 
  65.       * @throws Exception 
  66.       */  
  67.      public ActionForward add(ActionMapping mapping, ActionForm form,  
  68.                    HttpServletRequest request, HttpServletResponse response)  
  69.                    throwsException {  
  70.              
  71.             //获取从页面表单中提交过来的值  
  72.             UserActionForm uaf = (UserActionForm)form;  
  73.             Useruser = new User();  
  74.             BeanUtils.copyProperties(user,uaf);  
  75.             user.setCreateDate(newDate());  
  76.              
  77.             //调用业务逻辑操作  
  78.             UserManager.getInstance().addUser(user);  
  79.             returnmapping.findForward("add_success");    }  
  80.    
  81.      /** 
  82.       * 修改用户 
  83.       * @param mapping 
  84.       * @param form 
  85.       * @param request 
  86.       * @param response 
  87.       * @return 
  88.       * @throws Exception 
  89.       */  
  90.      public ActionForward modify(ActionMapping mapping, ActionForm form,  
  91.                    HttpServletRequestrequest, HttpServletResponse response)  
  92.                    throwsException {  
  93.             //获取从页面表单中提交过来的值  
  94.             UserActionForm uaf = (UserActionForm)form;  
  95.             User user = new User();  
  96.             BeanUtils.copyProperties(user,uaf);  
  97.              
  98.             //调用业务逻辑操作  
  99.             UserManager.getInstance().modifyUser(user);  
  100.             returnmapping.findForward("modify_success");  
  101.      }  
  102.       
  103.      /** 
  104.       * 根据ID查询用户 
  105.       * 
  106.       * @param mapping 
  107.       * @param form 
  108.       * @param request 
  109.       * @param response 
  110.       * @return 
  111.       * @throws Exception 
  112.       */  
  113.      public ActionForward find(ActionMapping mapping, ActionForm form,  
  114.                    HttpServletRequestrequest, HttpServletResponse response)  
  115.                    throwsException {  
  116.             //获取从页面表单中提交过来的值  
  117.             UserActionForm uaf = (UserActionForm)form;  
  118.              
  119.             String userId = uaf.getUserId();  
  120.              
  121.             //调用业务逻辑操作  
  122.             User user = UserManager.getInstance().findUserById(userId);  
  123.              
  124.             //将user对象从Action传递到JSP页面  
  125.             request.setAttribute("user",user);  
  126.              
  127.             returnmapping.findForward("find_success");  
  128.      }  
  129.       
  130. }  
  131.    


Struts-config.xml配置文件代码如下所示。

  1. <struts-config>  
  2.      
  3.     <form-beans>  
  4.             <form-bean name="userForm"type="com.bjpowernode.drp.web.forms.UserActionForm"/>  
  5.     </form-beans>  
  6.      
  7.     <action-mappings>  
  8.         <action path="/user/user_maint"  
  9.                       type="com.bjpowernode.drp.web.actions.UserAction"  
  10.                       name="userForm"  
  11.                       scope="request"  
  12.                       parameter="command"  
  13.         >  
  14.                 
  15.                <forward name="list_success" path="/user/user_list.jsp"/>  
  16.                <forward name="del_success" path="/user/user_maint.do?command=list"redirect="true"/>  
  17.                <forward name="add_success"  path="/user/user_maint.do?command=list"redirect="true"/>  
  18.                <forward name="modify_success" path="/user/user_maint.do?command=list"redirect="true"/>  
  19.                <forward name="find_success"  path="/user/user_modify.jsp"/>  
  20.   </action-mappings>    
  21. </struts-config>  


其中配置Action的时候,配置了parameter属性,并且指定了parameter属性值为command,当用户单击添加或删除用户操作时,会以http://localhost:8080/struts_dispatchaction_usermgr/user/user_maint.do?command=list,这个请求会被映射到UserAction控制器中,Struts根据method参数的值把这个请求发送到控制器UserAction的list方法。这样取得参数完成页面的调用。

从上述可以看出,DispatchAction可以通过command这个参数的值来决定调用DispatchAction的哪个方法,DispatchAction是从用户请求的URL中提取parameter定义参数的值,从而决定调用哪个方法处理用户请求。所以DispatchAction不能通过ActionForm向服务器提交请求,因为提交表单的时候不能向服务器传递参数。

根据上述示例我们可以总结出DispatchAction与Action区别:Action是从表单中取得数据,并且自动转换为对应的类型。而DispatchAction取得配置文件中parameter,截取parameter定义的参数值。但是DispatchAction可以处理多个动作而不需要建立多个Action。

DispatchAction可以在同一个控制器中处理多个动作,但只能通过URL来调用控制器,根据用户提交的参数来决定调用哪个方法来处理用户请求。不能通过表单提交用户请求信息,在struts中如果要在同一个表单中处理不同的动作,可以使用LookupDispatchAction。在这里就不详细讲述了,有兴许的童鞋可以查找些资料来实现。

下一篇struts页面转发控制ActionForward和ActionMapping。
### ECharts `dispatchAction` 方法详解 #### 什么是 `dispatchAction` `dispatchAction` 是 ECharts 提供的一个方法,用于手动触发视图中的交互行为。通过该方法,开发者可以在不依赖用户操作的情况下模拟某些交互动作,比如高亮某个数据项、选中某一部分或者跳转到特定的时间范围。 此功能特别适用于动态调整图表状态或实现复杂的交互逻辑[^1]。 --- #### `dispatchAction` 的基本语法 以下是 `dispatchAction` 的通用调用方式: ```javascript myChart.dispatchAction({ type: '某种交互类型', // 如 highlight, select 等 seriesIndex: 0, // 可选参数,指定作用于哪个系列 dataIndex: 0 // 可选参数,指定作用于哪条数据 }); ``` 其中,`type` 参数决定了具体执行的动作类型,而其他参数则根据不同的 `type` 进行补充说明。 --- #### 常见的 `type` 类型及其用途 | **Type** | **描述** | |----------------|---------------------------------------------------------------------------------------------| | `highlight` | 高亮指定的数据项 | | `downplay` | 取消高亮指定的数据项 | | `select` | 选中指定的数据项 | | `unselect` | 取消选中指定的数据项 | | `legendSelect`| 控制图例的选择状态 | | `dataZoom` | 调整数据区域缩放 | 这些类型的详细解释如下: - **Highlight 和 Downplay** 当需要突出显示某一数据点时,可以使用 `highlight`;反之,则使用 `downplay` 来取消这种效果。 示例代码: ```javascript myChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: 2 }); ``` - **Select 和 Unselect** 如果希望让用户能够选择部分数据并对其进行特殊处理(如统计分析),可以通过这两个命令来控制。 示例代码: ```javascript myChart.dispatchAction({ type: 'select', seriesIndex: 0, name: '北京' }); ``` - **Legend Select** 图例的操作也可以被程序化地管理,允许隐藏或展示对应的序列。 示例代码: ```javascript myChart.dispatchAction({ type: 'legendSelect', name: '类别A' }); ``` - **Data Zoom** 对于时间轴或其他连续维度上的数据集,可能需要改变可视化的窗口大小。此时可以用 `dataZoom` 动作完成这一需求。 示例代码: ```javascript myChart.dispatchAction({ type: 'dataZoom', startValue: new Date('2023-01-01'), endValue: new Date('2023-06-01') }); ``` 以上每种类型都支持额外传递一些选项来自定义其表现形式[^2]。 --- #### 解决常见问题 ##### Q1: 执行 `dispatchAction` 后无反应? 这通常是因为目标对象未正确定义好,或者是图表尚未完全渲染完毕就发起了请求。建议确认以下几点: 1. 是否正确初始化了 chart 实例; 2. 数据源是否已经加载成功; 3. 尝试延迟几毫秒再发起 action 请求以等待 DOM 更新完成。 ##### Q2: 怎样批量应用多个 actions? 如果一次想要发送多组指令给同一个实例,推荐利用循环结构逐一调用即可。例如: ```javascript const actions = [ { type: 'highlight', seriesIndex: 0, dataIndex: 1 }, { type: 'select', seriesIndex: 1, name: '上海' } ]; actions.forEach(action => { myChart.dispatchAction(action); }); ``` ##### Q3: 如何监听外部事件联动 charts 行为? 结合 HTML/CSS/JavaScript 中的各种机制,我们可以轻松捕捉键盘按键、鼠标点击甚至手势滑动等信号,并将其转化为相应的图表响应。下面是一个简单的例子演示如何绑定按钮控件至图表刷新过程: HTML 结构片段: ```html <button id="btn">切换模式</button> <div id="main"></div> <script src="./path/to/echarts.min.js"></script> ``` JS 处理脚本: ```javascript document.getElementById('btn').addEventListener('click', function () { const currentMode = myChart.getOption().series[0].mode; let nextMode = (currentMode === 'stack') ? 'group' : 'stack'; myChart.setOption({ series: [{ mode: nextMode }] }, true); myChart.dispatchAction({ type: 'refresh' }); }); ``` 上述案例展示了如何通过按钮切换堆叠柱状图和平铺柱形图之间的转换。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值