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>
   <?xml:namespace prefix = fmt />

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章