LookupDispatchAction使用示例(中文)(轉(zhuǎn))
Posted on 2006-07-20 18:04 duyouyou.com 閱讀(3099) 評(píng)論(0) 編輯 收藏 所屬分類: web技術(shù)org.apache.struts.actions.LookupDispatchAction類別是 DispatchAction 類別的子類,與DispatchAction類似的是,它透過(guò)請(qǐng)求上的參數(shù)來(lái)決定該執(zhí)行哪一個(gè)方法,不過(guò)LookupDispatchAction多了查 詢訊息資源檔案的功能,LookupDispatchAction的用處之一,就是當(dāng)一個(gè)表單中包括兩個(gè)以上的按鈕時(shí),可以透過(guò)查詢訊息資源檔來(lái)確定相對(duì) 應(yīng)的動(dòng)作。
直接以實(shí)例來(lái)說(shuō)明,在繼承LookupDispatchAction之後,您要重新定義getKeyMethodMap()方法,并定義好自己的相關(guān)處理方法,例如:
假設(shè)訊息資源檔中包括以下的訊息:
為了要使用LookupDispatchAction,在struts-config.xml中定義請(qǐng)求參數(shù)中該有的名稱:
現(xiàn)在假設(shè)您的表單頁(yè)面包括以下的內(nèi)容:
當(dāng)您按下任一個(gè)按鈕時(shí),請(qǐng)求參數(shù)中會(huì)包括method=Save或是method=Preview或是method= Reset,假設(shè)是method=Save好了,LookupDispatchAction會(huì)根據(jù)它作為value,在訊息資訊檔找到對(duì)應(yīng)的key,然後 根據(jù)key與getKeyMethodMap()得知要執(zhí)行的方法為save()方法。
那么關(guān)於國(guó)際化訊息管理的部份呢?例如想要在表單按鈕上使用中文?
一樣的,您的訊息檔案中必須包括下面的內(nèi)容:
然後,您要使用native2ascii將訊息檔案轉(zhuǎn)換為Unicode編碼,例如:
接下來(lái)的問(wèn)題是,瀏覽器發(fā)送過(guò)來(lái)的中文參數(shù),為了要能正確的解析,要使用request的 setCharacterEncoding("Big5"),這樣才能得到正確的中文參數(shù),但是在什么地方作這個(gè)動(dòng)作?您可以在Servlet Filter中作這件事,另一個(gè)可能的地方則是 ActionForm 的reset()方法中,例如:
這樣您的按鈕就可以使用中文訊息了。
如果您愿意的話,可以搭配使用 <bean:message> 來(lái)使用上述的功能,直接由標(biāo)簽來(lái)管理訊息檔案中的訊息。
直接以實(shí)例來(lái)說(shuō)明,在繼承LookupDispatchAction之後,您要重新定義getKeyMethodMap()方法,并定義好自己的相關(guān)處理方法,例如:
- EditAction.java
package onlyfun.caterpillar;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.*;
public class EditAction extends LookupDispatchAction {
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.save", "save");
map.put("button.preview", "preview");
map.put("button.reset", "reset");
return map;
}
public ActionForward save(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// ......
}
public ActionForward preview(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// ......
}
public ActionForward reset(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// ......
}
}
假設(shè)訊息資源檔中包括以下的訊息:
- messages.properties
button.save=Save
button.preview=Preview
button.reset=Reset
為了要使用LookupDispatchAction,在struts-config.xml中定義請(qǐng)求參數(shù)中該有的名稱:
- struts-config.xml
...
<action path="/edit"
type="onlyfun.caterpillar.EditAction"
parameter="method"
name="editForm"/>
...
現(xiàn)在假設(shè)您的表單頁(yè)面包括以下的內(nèi)容:
...
?<form name="editForm" method="post"
?????? action="/strutsapp/edit.do">
??? .....
??? <input type="submit" name="method" value="Save"/>
??? <input type="submit" name="method" value="Preview"/>
??? <input type="submit" name="method" value="Reset"/>
?</form>
...
?<form name="editForm" method="post"
?????? action="/strutsapp/edit.do">
??? .....
??? <input type="submit" name="method" value="Save"/>
??? <input type="submit" name="method" value="Preview"/>
??? <input type="submit" name="method" value="Reset"/>
?</form>
...
當(dāng)您按下任一個(gè)按鈕時(shí),請(qǐng)求參數(shù)中會(huì)包括method=Save或是method=Preview或是method= Reset,假設(shè)是method=Save好了,LookupDispatchAction會(huì)根據(jù)它作為value,在訊息資訊檔找到對(duì)應(yīng)的key,然後 根據(jù)key與getKeyMethodMap()得知要執(zhí)行的方法為save()方法。
那么關(guān)於國(guó)際化訊息管理的部份呢?例如想要在表單按鈕上使用中文?
?...
?<form name="editForm" method="post"
?????? action="/strutsapp/edit.do">
??? .....
??? <input type="submit" name="method" value="存檔"/>
??? <input type="submit" name="method" value="預(yù)覽"/>
??? <input type="submit" name="method" value="重設(shè)"/>
?</form>
...
?<form name="editForm" method="post"
?????? action="/strutsapp/edit.do">
??? .....
??? <input type="submit" name="method" value="存檔"/>
??? <input type="submit" name="method" value="預(yù)覽"/>
??? <input type="submit" name="method" value="重設(shè)"/>
?</form>
...
一樣的,您的訊息檔案中必須包括下面的內(nèi)容:
- messages.properties
button.save=存檔
button.preview=預(yù)覽
button.reset=重設(shè)
然後,您要使用native2ascii將訊息檔案轉(zhuǎn)換為Unicode編碼,例如:
native2ascii messages_zh_TW.txt messages_zh_TW.properties |
接下來(lái)的問(wèn)題是,瀏覽器發(fā)送過(guò)來(lái)的中文參數(shù),為了要能正確的解析,要使用request的 setCharacterEncoding("Big5"),這樣才能得到正確的中文參數(shù),但是在什么地方作這個(gè)動(dòng)作?您可以在Servlet Filter中作這件事,另一個(gè)可能的地方則是 ActionForm 的reset()方法中,例如:
package onlyfun.caterpillar;
public class UserForm extends ActionForm {
???? ......
??? public void reset(ActionMapping mapping,
???????????????????? HttpServletRequest request) {
??????? try {
??????????? request.setCharacterEncoding("Big5");
??????????? .......
??????? }
??????? catch(Exception e) {
??????????? ....
??????? }
?? }
}
?public class UserForm extends ActionForm {
???? ......
??? public void reset(ActionMapping mapping,
???????????????????? HttpServletRequest request) {
??????? try {
??????????? request.setCharacterEncoding("Big5");
??????????? .......
??????? }
??????? catch(Exception e) {
??????????? ....
??????? }
?? }
}
這樣您的按鈕就可以使用中文訊息了。
如果您愿意的話,可以搭配使用 <bean:message> 來(lái)使用上述的功能,直接由標(biāo)簽來(lái)管理訊息檔案中的訊息。