LookupDispatchActionSupport和DispatchActionSupport的最大不同

LookupDispatchActionSupport和DispatchActionSupport均可在org.springframework.web.struts下找到

1、LookupDispatchActionSupport
前者比后者多了一个Map,对于前者,哪个key对应的方法名是在这个Map中取的,而这个map又要对应一个源文件,可以是class的,也可以是properties的,所以说,要用前者,则必须有一个资源文件,jsp页面上显示的是value(即资源文件中key/value中的value),而在Action类中Map对象中的key却是资源文件中的key,举个例子:

例子一、按钮的写法

以下是资源文件:

sendPage.send=send
sendPage.save=save

设置好资源文件后,在jsp页面上要写两个提交按钮:send和save
那么就应该这么写:
<input type=”submit” name=”method” value=”send”>
<input type=”submit” name=”method” value=”save”>

这里的method须在struts-config.xml中<action>中的parameter属性中配置,如下:

  <action path="/messageAction"

          type="com.ztesoft.ds.application.web.message.MessageDispatchAction"

          name="MessageSendForm"

          parameter="method"/>

当提交时,method要么等于send,要么等于save,提交到action后,在LookupDispatchActionSupport中通过getKeyMethodMap调用相关方法,getKeyMethodMap方法中应该这么写:

  protected Map getKeyMethodMap() {
    Map map = new HashMap();
    map.put("sendPage.send", "postMessage");
    map.put("sendPage.save", "saveMessage");
    return map;
  }

其中,map中的key就是资源文件中的key,value是在该action中的方法
探测到用了哪个key,就调用对应的方法
例子二、url的写法
在jsp页面中,通常还通过url调用下一页面,在LookupDispatchActionSupport中,是这么写的:
资源文件:
sendPage.link=link
那么,在你的url中就要这么写:
url="<%=request.getContextPath()%>/messageAction.do?method=link";
其中,action的配置和上面的一样,parameter要设为method,和url中的要保持一致
然后转发到action里面,它的getKeyMethodMap应该这么写:

 protected Map getKeyMethodMap() {
    Map map = new HashMap();
    map.put("sendPage.link", "linkMessage");//linkMessage是方法名,sendPage.link是资源文件中的key
    return map;
  }


2、DispatchActionSupport

要是用DispatchActionSupport,就没有那么麻烦了,只需要在url中这么写即可:
url="<%=request.getContextPath()%>/messageAction.do?method=linkMessage";//linkMessage是方法名
但有一点和上面一样,那就是action的parameter要设为method(你也可以设成别的,但是一定要和jsp页面保持一致)。

但是这种有一个致命的缺点,那就是不能在页面上有提交按钮
其实,dispatch虽然不能通过直接用submit按纽实现提交或多提交,但是还是可以通过button按纽、javascript方法实现多提交,不过这样页面看起来不干净,不爽。
lookupdispatch中的多提交就可以这么写:
<html:submit property="method" styleClass="buttons">
      <fmt:message key="sendPage.send"/>
</html:submit>
<html:submit property="method" styleClass="buttons">
     <fmt:message key="sendPage.save"/>
 </html:submit>
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值