2007-05-21
webwork自带的RestfulActionMapper弱了点
Webwork 自带的 RestfulActionMapper弱了点
首先, 像图片,js,css 等资源文件不大好印射(会出现 action 找不到错误)
不支持namespace
反向解析url 时,忽略了除id 以外的变量
于是我写了一个稍微好点的 RestfulActionMapper, 也符合了我现在项目的要求
其代码如下
首先, 像图片,js,css 等资源文件不大好印射(会出现 action 找不到错误)
不支持namespace
反向解析url 时,忽略了除id 以外的变量
于是我写了一个稍微好点的 RestfulActionMapper, 也符合了我现在项目的要求
其代码如下
java 代码
- package com.longthsoft.hellomobile.webwork;
- import java.net.URLDecoder;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import com.opensymphony.webwork.RequestUtils;
- import com.opensymphony.webwork.dispatcher.mapper.ActionMapper;
- import com.opensymphony.webwork.dispatcher.mapper.ActionMapping;
- import com.opensymphony.webwork.dispatcher.mapper.RestfulActionMapper;
- import com.opensymphony.xwork.config.ConfigurationManager;
- import com.opensymphony.xwork.config.RuntimeConfiguration;
- public class BetterRestfulActionMapper implements ActionMapper {
- protected static final Log LOG = LogFactory.getLog(RestfulActionMapper.class);
- private static final String ID = "id";
- public ActionMapping getMapping(HttpServletRequest request) {
- String uri = RequestUtils.getServletPath(request);
- if (uri.indexOf('.', uri.length() - 5) != -1) {
- return null;
- }
- int pos1 = uri.indexOf('/', 1);
- if (pos1 == -1) {
- return null;
- }
- String part1 = uri.substring(1, pos1);
- String namespace = "";
- String actionName;
- int pos2 = uri.indexOf('/', pos1 + 1);
- if (pos2 == -1) {
- actionName = part1;
- uri = uri.substring(pos1 + 1);
- } else {
- RuntimeConfiguration config = ConfigurationManager.getConfiguration()
- .getRuntimeConfiguration();
- String part2 = uri.substring(pos1 + 1, pos2);
- if (config.getActionConfigs().containsKey("/" + part1)) {
- namespace = "/" + part1;
- actionName = part2;
- uri = uri.substring(pos2 + 1);
- } else {
- actionName = part1;
- uri = uri.substring(pos1 + 1);
- }
- }
- List<String> list = new ArrayList<String>(Arrays.asList(uri.split("/")));
- if (list.size() % 2 == 1) {
- list.add(0, ID);
- }
- HashMap<String, String> params = new HashMap<String, String>();
- for (int i = 0; i < list.size() / 2; ++i) {
- try {
- String name = URLDecoder.decode(list.get(2 * i), "UTF-8");
- String value = URLDecoder.decode(list.get(2 * i + 1), "UTF-8");
- params.put(name, value);
- } catch (Exception ex) {
- LOG.warn(ex);
- }
- }
- return new ActionMapping(actionName, namespace, "", params);
- }
- public String getUriFromActionMapping(ActionMapping mapping) {
- String uri = mapping.getNamespace() + "/" + mapping.getName();
- Map params = mapping.getParams();
- if (params.containsKey(ID)) {
- uri += "/" + params.get(ID);
- }
- for (Object o : mapping.getParams().entrySet()) {
- Entry entry = (Entry) o;
- String name = (String) entry.getKey();
- if (!name.equals(ID)) {
- uri += "/" + name + "/" + entry.getValue();
- }
- }
- return uri;
- }
- }
- 浏览: 30627 次
- 性别:

- 来自: 浙江台州

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
TableViewer, TreeViewer ...
不用点击,直接以编辑模式展现所有CELL如何实现?
-- by tanchang18 -
让ToolBarManager中的项不 ...
你太强啦博主!
-- by 379548695 -
TableViewer, TreeViewer ...
想问下楼主,treeviewer能支持多级树不能?
-- by 379548695 -
RCP开发日积月累
"关于SWT Table中, 加入其他控件 (2006-9-2) SWT ...
-- by younghaowei -
照着葫芦画,CComboViewer
nice,为啥么不上个图看看效果。
-- by semicircle






评论排行榜