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;
- }
- }