`

LookupDispatchAction的使用

阅读更多
转载:http://hi.baidu.com/javajavajava/blog
org.apache.struts.actions.LookupDispatchAction类别是DispatchAction类别的子类,与DispatchAction类似的是,它透过请求上的参数来决定该执行哪一个方法,不过LookupDispatchAction多了查询讯息资源文件的功能,LookupDispatchAction的用处之后,就是当一个窗体中包括两个以上同名的送出按钮时,可以透过查询讯息资源文件来确定相对应的动作。

直接以实例来说明,在继承LookupDispatchAction之后,您要重新定义getKeyMethodMap()方法,并定义好自己的相关处理方法,例如:

代码:
package onlyfun.caterpillar; 
                                                                                 
import javax.servlet.http.*; 
import org.apache.struts.action.*; 
import org.apache.struts.actions.*; 
                                                                                 
public class ShoppingAction extends LookupDispatchAction { 
     protected Map getKeyMethodMap() { 
         Map map = new HashMap(); 
         map.put("button.continue", "continue"); 
         map.put("button.checkout", "checkout"); 
         return map; 
     } 

     public ActionForward continue(ActionMapping mapping, 
                               ActionForm form, 
                               HttpServletRequest request, 
                               HttpServletResponse response) 
     throws Exception { 
         // ...... 
     } 

      public ActionForward checkout(ActionMapping mapping, 
                               ActionForm form, 
                               HttpServletRequest request, 
                               HttpServletResponse response) 
     throws Exception { 
         // ...... 
     }                                                               
} 


假设讯息资源文件中包括以下的讯息:

代码:
button.continue=Continue 
button.checkout=Checkout 

为了要使用LookupDispatchAction,我们如同DispatchAction一样在struts-config.xml中定义请求参数中该有的名称:

代码:
 <action path="/shopping" 
  type="onlyfun.caterpillar.ShoppingAction" 
  parameter="method" 
  name="cartForm"/> 

现在假设您的窗体页面包括以下的内容:
代码:
  <html:form action="/shopping"> 
         <html:submit property="method"> 
             <bean:message key="button.continue"/> 
         </html:submit> 
         <html:submit property="method"> 
             <bean:message key="button.checkout"/> 
         </html:submit> 
     </html:form> 

这些Struts自订卷标在执行后会产生以下的内容:
代码:
<form name="cartForm" method="post" action="/HelloStruts/shopping.do"> 
     <input type="submit" name="method" value="Continue"/> 
     <input type="submit" name="method" value="Checkout"/> 
</form>

所以当您按下任一个按钮时,请求参数中会包括method=Continue或是method=Checkout,假设是method=Continue

好了,LookupDispatchAction会根据它作为value,在讯息信息文件找到对应的key,然后根据key与

getKeyMethodMap()得知要执行的方法为continue()方法。


********************************************************
Struts DispatchAction的如何在表单带method参数提交
DispatchAction是Struts包含的另一个能大量节省开发时间的Action类我想用DispatchAction来做一个从表单添加,修改,删除记录的功能
<html:form action="/MyDispatchAction">

MyDispatchAction中有add,alert,delete等方法,问题是如何让表单提交的时候加上参数呢?
比如:按下add button实现 MyDispathAction?method=add这样的一次提交?


1.使用 DispatchAction
DispatchAction是Struts包含的另一个能大量节省开发时间的Action类。与其它Action类仅提供单个execute()方法实现单个业务不同,DispatchAction允许你在单个Action类中编写多个与业务相关的方法。这样可以减少Action类的数量,并且把相关的业务方法集合在一起使得维护起来更容易。

要使用DispatchAction的功能,需要自己创建一个类,通过继承抽象的DispatchAction得到。对每个要提供的业务方法必须有特定的方法signature。例如,我们想要提供一个方法来实现对购物车添加商品清单,创建了一个类ShoppingCartDispatchAction提供以下的方法:

那么,这个类很可能还需要一个deleteItem()方法从客户的购物车中删除商品清单,还有clearCart()方法清除购物车等等。这时我们就可以把这些方法集合在单个Action类,不用为每个方法都提供一个Action类。

在调用ShoppingCartDispatchAction里的某个方法时,只需在URL中提供方法名作为参数值。就是说,调用addItem()方ǖ?URL看起来可能类似于:

http://myhost/storefront/action/cart?method=addItem

其中method参数指定ShoppingCartDispatchAction中要调用的方法。参数的名称可以任意配置,这里使用的"method"只是一个例子。参数的名称可以在Struts配置文件中自行设定。

=================================================================
2.使用 LookupDispatchAction
org.apache.struts.actions.LookupDispatchAction类:

通常LookupDispatchAction主要应用于在一个表单中有多个提交按钮,而这些按钮又有一个共同的名字的场合,这些按钮的名字和具体的ActionMapping的parameter属性相对应。

配置LookupDispatchAction时,应该在<action>元素中,把parameter属性设置为"action",使它和<html:submit>标签的property属性相一致。

做一个隐藏变量就可以了
然后用JS判断一下
<SCRIPT LANGUAGE="javascript"> 
<!-- 
function SetAction(opType) { 
document.name1.action.value = opType 
document.name1.submit(); 
} 
//--> 
</SCRIPT> 
</head> 
<body> 

<html:form name="name1" action="/Del" type="XX.XX.Del"> 
<html:hidden property="action" /> 
<html:button property="update" value="UDDATE" 
onclick="SetAction’updateDsp’);"></html:button> 
<html:button property="add" value="ADD" 
onclick="SetAction’addDsp’);"></html:button> 
</html:form> 
</body> 
</HTML> 

定义一个hidden的元素,JS控制提交的参数

用这个类用多了就比较乱了,form的表单映射会烦死你。
同样用楼上老兄的方法就可以,这个用在同个页面上多个提交时候使用比较合适。

如果仅仅是一个页面一个提交的话,还有个更简单的方法。
<html:form action="/MyDispatchAction?action=add">

加个参数不就行了么~在action里面验证这个参数,如果有form的话加个getset方法同样验证加个注释也可以解决问题。


An abstract Action that dispatches to the subclass mapped execute method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter property of the corresponding ActionMapping. To configure the use of this action in your struts-config.xml file, create an entry like this:

<action path="/test"
type="org.example.MyAction"
name="MyForm"
scope="request"
input="/test.jsp"
parameter="method"/>
which will use the value of the request parameter named "method" to locate the corresponding key in ApplicationResources. For example, you might have the following ApplicationResources.properties:

button.add=Add Record
button.delete=Delete Record
And your JSP would have the following format for submit buttons:

<html:form action="/test">
<html:submit property="method">
<bean:message key="button.add"/>
</html:submit>
<html:submit property="method">
<bean:message key="button.delete"/>
</html:submit>
</html:form>
Your subclass must implement both getKeyMethodMap and the methods defined in the map. An example of such implementations are:

protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.add", "add");
map.put("button.delete", "delete");
return map;
}

public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do add
return mapping.findForward("success");
}

public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do delete
return mapping.findForward("success");
}


Notes - If duplicate values exist for the keys returned by
getKeys, only the first one found will be returned. If no corresponding key
is found then an exception will be thrown. You can override the
method unspecified to provide a custom handler. If the submit
was cancelled (a html:cancel button was pressed), the custom
handler cancelled will be used instead.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics