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 {
// ......
}
}
button.save=Save button.preview=Preview button.reset=Reset为了要使用LookupDispatchAction,在struts-config.xml中定义请求参数中该有的名称:
...现在假设您的窗体页面包括以下的内容:
<action path="/edit"
type="onlyfun.caterpillar.EditAction"
parameter="method"
name="editForm"/>
...
... <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> ...当您按下任一个按钮时,请求参数中会包括method=Save或是method=Preview或是method= Reset,假设是method=Save好了,LookupDispatchAction会根据它作为value,在讯息信息文件找到对应的key,然后 根据key与getKeyMethodMap()得知要执行的方法为save()方法。
... <form name="editForm" method="post" action="/strutsapp/edit.do"> ..... <input type="submit" name="method" value="存档"/> <input type="submit" name="method" value="预览"/> <input type="submit" name="method" value="重设"/> </form> ...一样的,您的讯息档案中必须包括下面的内容:
button.save=存檔 button.preview=預覽 button.reset=重設然后,您要使用native2ascii将讯息档案转换为Unicode编码,例如:
native2ascii messages_zh_TW.txt messages_zh_TW.properties |
package onlyfun.caterpillar; public class UserForm extends ActionForm { ...... public void reset(ActionMapping mapping, HttpServletRequest request) { try { request.setCharacterEncoding("Big5"); ....... } catch(Exception e) { .... } } }
这样您的按钮就可以使用中文讯息了。
如果您愿意的话,可以搭配使用 <bean:message> 来使用上述的功能,直接由卷标来管理讯息档案中的讯息。