DispatchAction provides a mechanism for grouping a set of related functions into a single action, thus eliminating the need to create seperate actions for each functions. In this example we will see how to group a set of user related actions like add user, update user and delete user into a single action called UserAction.
DisPatchAction 提供了一种把一系列相关功能都分组到一个action里面的机制,这样就消除了为每一个功能都创建一个单独的Action的需要。在这个例子中, 我们将看到如何设置一组User相关的Action,一个单独的Action的需要。在这个例子中, 我们将看到如何设置一组User相关的Action,像增加User,修改User,删除User都放在一个命名为UserAction的action 里。
The class UserAction extends org.apache.struts.actions.DispatchAction. This class does not provide an implementation of the execute() method as the normal Action class does. The DispatchAction uses the execute method to manage delegating the request to the individual methods based on the incoming request parameter. For example if the incoming parameter is "method=add", then the add method will be invoked. These methods should have similar signature as the execute method.
这个 UserAction类继承了org.apache.struts.actions.DispatchAction。这个类没有像普通的Action类一样提供一个实现的execute方法。DispatchAction使用execute方法根据传入的request参数来管理代表这个request的单个方法。例如:如果传入的参数是"method=add", 那么这个add方法将被调用。这些方法应该像execute方法一样有类似的特征。
01.public class UserAction extends DispatchAction {
02.
03. private final static String SUCCESS = "success";
04.
05. public ActionForward add(ActionMapping mapping, ActionForm form,
06. HttpServletRequest request, HttpServletResponse response)
07. throws Exception {
08. UserForm userForm = (UserForm) form;
09. userForm.setMessage("Inside add user method.");
10. return mapping.findForward(SUCCESS);
11. }
12.
13. public ActionForward update(ActionMapping mapping, ActionForm form,
14. HttpServletRequest request, HttpServletResponse response)
15. throws Exception {
16. UserForm userForm = (UserForm) form;
17. userForm.setMessage("Inside update user method.");
18. return mapping.findForward(SUCCESS);
19. }
20.
21. public ActionForward delete(ActionMapping mapping, ActionForm form,
22. HttpServletRequest request, HttpServletResponse response)
23. throws Exception {
24. UserForm userForm = (UserForm) form;
25. userForm.setMessage("Inside delete user method.");
26. return mapping.findForward(SUCCESS);
27. }
28.}
If you notice the signature of the add, update and delete methods are similar to the execute method except the name. The next step is to create an action mapping for this action handler. The request parameter name is specified using the parameter attribute. Here the request parameter name is method.
如果你发现 add,update和delete的方法都类似execute方法,除了方法名以外。下一步是为这个action处理程序创建一个action mapping。request参数的名称指定使用parameter属性。 在这里request参数名称是method。
1.<action-mappings>
2. <action input="/index.jsp" parameter="method" name="UserForm" path="/UserAction" scope="session" type="com.vaannila.UserAction">
3. <forward name="success" path="/index.jsp" />
4. </action>
5.</action-mappings>
Now lets see how to invoke a DispatchAction from jsp. We have a simple form with three buttons to add, update and delete a user. When each button is clicked a different method in UserAction class is invoked.
现在让我们看看从JSP如何调用一个DispatchAction。我们有一个简单的表单,有三个按钮用来添加,更新和删除用户。 当每个按钮被点击,就会调用UserAction类的不同的方法。
01.<html>
02.<head>
03.<script type="text/javascript">
04.function submitForm()
05.{
06.document.forms[0].action = "UserAction.do?method=add"
07.document.forms[0].submit();
08.}
09.</script>
10.</head>
11.<body>
12.<html:form action="https://youtubeproxy.org/default.aspx
" >
13.<table>
14.<tr>
15. <td>
16. <bean:write name="UserForm" property="message" />
17. </td>
18.</tr>
19.<tr>
20. <td>
21. <html:submit value="Add" onclick="submitForm()" />
22. </td>
23.</tr>
24.<tr>
25. <td>
26. <html:submit property="method" value="update" />
27. </td>
28.</tr>
29.<tr>
30. <td>
31. <html:submit property="method" >delete</html:submit>
32. </td>
33.</tr>
34.</table>
35.</html:form>
36.</body>
37.</html>
Now consider the update and the delete button. The request parameter name specified in the action handler is "method". So this should be specified as the property name for the submit button. The name of the method to be invoked and the value of the button should be the same. So when the button is clicked the corresponding method in the UserAction will be called. The delete button shows an alternate way to specify the value of the button.
现在考虑更新和删除按钮。 request参数的名称在指定的action处理程序中是“method”。 所以这应该被指定作为提交按钮的属性名。 被调用的方法名要和该按钮的值相同。 因此,当按钮被点击UserAction里相应的方法将被调用。 删除按钮显示了另一种方法来指定按钮的值。
Here the main constraint is the method name and the button name should be same. So we can't have an update button like this "Update". Inorder to avoid this you can call a javascript function on click of the button. Specify the action and submit the form from javascript. In this way we can have a different button name and method name. On click of the Add button the action value is set to "UserAction.do?method=add" and the form is submitted from javascript.
这里的主要制约因素是方法名称和按钮名称应该是相同的。 这样的话,我们就不能有一个像"Update"这样的修改按钮。 为了避免这个,当点击按钮的时候你可以调用一个JavaScript函数。指定action并以javascript方式提交表单。 这样,我们就可以有不同的按钮名称和方法名称了。点击添加按钮,action的值设被置为"UserAction.do?method=add"并且表单是以JavaScript的方式提交的。
分享到:
相关推荐
public class MyDispatchAction extends DispatchAction { public ActionForward executeSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ...
public class MyDispatchAction extends DispatchAction { public ActionForward method1(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...
public class MyDispatchAction extends DispatchAction { public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ...
public class ArticleAction extends DispatchAction { /** * 文章添加操作 */ public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response...
Method method = this.getClass().getMethod(methodName, new Class[]{ActionMapping.class, ActionForm.class, HttpServletRequest.class, HttpServletResponse.class}); return (String) method.invoke(this, ...
配置`DispatchAction`通常需要在struts-config.xml文件中定义一个action元素,并设置其class属性为`com.opensymphony.xwork DispatchAction`(或在Struts 1中为`org.apache.struts.actions.DispatchAction`)。...
public class YourDispatchAction extends DispatchAction { public ActionForward executeAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ...
<bean id="myAction" class="com.example.MyAction"> <!-- 注入所需的依赖 --> ``` 3. 在hibernate.cfg.xml中: ```xml <!-- 数据库连接信息 --> <!-- 实体映射配置 --> ``` 在压缩包文件"mypro"中,你...
public class UserAction extends DispatchAction { public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { // 实现列表展示的业务...
3. 对于每个处理方法,定义一个子Action,指定该方法的名称作为`action-class`的值,并设置`parameter`为对应的方法名。 例如: ```xml ``` 在这个例子中,`/usermgr`是Action的根路径,而`add`, `delete`, ...
public class MyActionForm extends ActionForm { private Map, String> parameters = new HashMap(); public Map, String> getParameters() { return parameters; } public void setParameters(Map, String>...
public class ShoppingCartAction extends DispatchAction { public String addBook() { // 添加书籍到购物车 return SUCCESS; } public String removeBook() { // 从购物车移除书籍 return SUCCESS; } } `...
public class BaseDispatchAction extends DispatchAction { protected WebApplicationContext ctx; protected UserManager userMgr; protected final Object getBean(String beanName) { return ctx.getBean...
推荐使用Struts的DispatchAction,因为它可以减少Spring配置中的工作量,提高代码的可维护性。 其次,Hibernate是一个强大的对象关系映射(ORM)框架,主要负责持久层的处理,实现了数据与业务逻辑的解耦。通过...
4. class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" 5. lazy-init="true"/> 6. 7. class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true"> 8. ...
public class BaseAction extends DispatchAction { public FdlUserService fus; public FdmShisanhachuService chus; public FdlMachineService mas; public FdlTenkaService tens; public FdmShisanService ...
Struts的ActionForm用于接收和验证用户输入,DispatchAction则负责处理请求并调用DAO层的方法。 对于添加、修改、删除部门和员工的功能,每个操作都需要对应的Action和ActionForm。例如,添加部门的ActionForm会...
public class ProcessAction extends DispatchAction { public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { return mapping....
public class LookupDispatchAction extends DispatchAction { public ActionForward _add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ...
public class EntryinfAction extends DispatchAction { // 处理首页请求 public ActionForward index(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { ...