Java技术专区--Hilly

http://blog.duyouyou.com
   :: 首页 :: 新随笔 :: 联系 ::  :: 管理

LookupDispatchAction使用示例(中文)(转)

Posted on 2006-07-20 18:04 duyouyou.com 阅读(3077) 评论(0)  编辑  收藏 所属分类: web技术
org.apache.struts.actions.LookupDispatchAction类别是 DispatchAction 类别的子类,与DispatchAction类似的是,它透过请求上的参数来决定该执行哪一个方法,不过LookupDispatchAction多了查 询讯息资源档案的功能,LookupDispatchAction的用处之一,就是当一个表单中包括两个以上的按钮时,可以透过查询讯息资源档来确定相对 应的动作。

直接以实例来说明,在继承LookupDispatchAction之後,您要重新定义getKeyMethodMap()方法,并定义好自己的相关处理方法,例如:
  • 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 {
// ......
}
}

假设讯息资源档中包括以下的讯息:
  • messages.properties
button.save=Save
button.preview=Preview
button.reset=Reset

为了要使用LookupDispatchAction,在struts-config.xml中定义请求参数中该有的名称:
  • 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>
...

一样的,您的讯息档案中必须包括下面的内容:
  • messages.properties
button.save=存档
button.preview=预览
button.reset=重设

然後,您要使用native2ascii将讯息档案转换为Unicode编码,例如:
native2ascii messages_zh_TW.txt messages_zh_TW.properties

接下来的问题是,浏览器发送过来的中文参数,为了要能正确的解析,要使用request的 setCharacterEncoding("Big5"),这样才能得到正确的中文参数,但是在什么地方作这个动作?您可以在Servlet Filter中作这件事,另一个可能的地方则是 ActionForm 的reset()方法中,例如:
package onlyfun.caterpillar;

public class UserForm extends ActionForm {
     ......

    public void reset(ActionMapping mapping,
                     HttpServletRequest request) {
        try {
            request.setCharacterEncoding("Big5");
            .......
        }
        catch(Exception e) {
            ....
        }
   }
}
 

这样您的按钮就可以使用中文讯息了。

如果您愿意的话,可以搭配使用 <bean:message> 来使用上述的功能,直接由标签来管理讯息档案中的讯息。


只有注册用户登录后才能发表评论。


网站导航:
 
Hold住