242010

最近在做一个推荐系统的项目,原来是设计前台一个表单对应后台一个form和action。无奈前台表单太多,只能把表单合并处理(在vo里都是对应同一个类,只是处理不同的逻辑)。这样就在前台表单里加了2个参数type和method。type用来区分form里的validate,然后利用method使用DispatchAction类。

采用 DispathAction
* 如果覆写DispathAction中的execute方法,必须显示的用super调用execute方法
* parameter参数值不能是execute或perform
* 了解<action>标签中的parameter的含义
* 了解DispathAction中的unspecified方法的含义

DispatchAction 的定义:
public abstract class DispatchAction extends Action
这是一个抽象的Action,它会根据request 中的parameter来执行相应的方法。通个这个Action类可以将不同的Action集中到一个Action文件中来。

Struts-config.xml:

<action path=”/saveSubscription” type=”org.apache.struts.actions.DispatchAction” name=”subscriptionForm” scope=”request” input=”/subscription.jsp” parameter=”method”/>

在Action中要有相应的方法:

Public class demoAction extends DispatchAction{

public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

}

你就可以通过这样的方法来访问你的程序:

http://localhost:8080/myapp/saveSubscription.do?method=update

如果parameter中参数为空,则执行Action中unspecified方法
DispatchAction就是在struts-config中用parameter参数 配置一个表单字段名,这个字段的值就是最终替代execute被调用的方法. 例如parameter=”method”而request.getParameter(“method”)=”save”,其中”save”就是 MethodName。struts的请求将根据parameter被分发到”save”或者”edit”或者什么。但是有一点,save()或者 edit()等方法的声明和execute必须一模一样。

DispatchAction的父类是 Action ,它的作用就在于将多个功能相似的业务逻辑放在同一个 Action 中实现,各个业务逻辑通过传入不同的参数来决定执行哪个操作方法

通常在 Action 中我们都是通过 execute 方法来处理业务逻辑及页面转向,一个 Action 只能完成一种业务逻辑处理 , 当然我们也可以在页面插入一个隐藏的变量,然后在 Action execute 方法中通过判断这个隐藏变量的值来决定调用哪个方法,也可以达到同 一个 Action 来处理多种业务逻辑

DispatchAction 是如何实现的?

比如对一个用户对象来说,存在增加,删除,修改的操作,首先创建一 个继承 DispatchAction UserAction 类,

然后将 addUser,delUser,updateUser 这些方法写在这个类里面,代码如下:

package com.why.struts.action;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.actions.DispatchAction;

import com.why.Constant;

import com.why.struts.form.AddUserForm;

import com.why.struts.form.LoginForm;

public class UserAction extends DispatchAction

{

public ActionForward addUser (ActionMapping mapping,ActionForm form,

HttpServletRequest request,HttpServletResponse response) throws Exception

{

// 增加用户业务的逻辑

return mapping.findForward(Constant. FORWARD_ADD );

}

public ActionForward delUser(ActionMapping mapping,ActionForm form,

HttpServletRequest request,HttpServletResponse response) throws Exception

{

// 删除用户业务的逻辑

return mapping.findForward(Constant. FORWARD_SUCCESS );

}

public ActionForward updateUser(ActionMapping mapping,ActionForm form,

HttpServletRequest request,HttpServletResponse response) throws Exception

{

// 更新用户业务的逻辑

return mapping.findForward(Constant. FORWARD_SUCCESS );

}

}

如何实现这些不同方法的调用呢 ? 那就是要在 struts-config.xml 文件中更改 action-mapping 的配置,如下:

< action-mappings >

< action

attribute = “addUserForm”

input = “/addUser.jsp”

name = “addUserForm”

parameter=”method”

path = “/addUser”

scope = “request”

type=”com.why.struts.action.UserAction” >

</ action >

< action

attribute = “delUserForm”

input = “/delUser.jsp”

name = “delUserForm”

parameter=”method”

path = “/delUser”

scope = “request”

type=”com.why.struts.action.UserAction” />

< action

attribute = “updateUserForm”

input = “/updateUser.jsp”

name = “updateUserForm”

parameter=”method”

path = “/updateUser”

scope = “request”

type=”com.why.struts.action.UserAction” />

</ action-mappings >

可以看到每个 <action  /> 中都增加了 parameter=” “ 项,这个值可以随便命名,如上面命名为 metho d ,用来接收页面传来的参数

如下为页面的提交, 注意:对应 <action  /> 中的 parameter , 对应 UserAction 类中的方法名

AddUser.jsp

<html:link href=”addUser.do?method=addUser“>Add User</html:link>

DelUser.jsp

<html:link href=”delUser.do?method=delUser“>Add User</html:link>

UpdateUser.jsp

<html:link href=”updateUser.do?method=updateUser“>Add User</html:link>

相关日志

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Protected by WP Anti Spam
© 2009 - 2024 冰河的博客