`
bencode
  • 浏览: 107213 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

webwork自带的RestfulActionMapper弱了点

    博客分类:
  • WEB
阅读更多
Webwork 自带的 RestfulActionMapper弱了点

首先, 像图片,js,css 等资源文件不大好印射(会出现 action 找不到错误)

不支持namespace

反向解析url 时,忽略了除id 以外的变量

于是我写了一个稍微好点的 RestfulActionMapper, 也符合了我现在项目的要求

其代码如下

java 代码
 
  1. package com.longthsoft.hellomobile.webwork;  
  2.   
  3. import java.net.URLDecoder;  
  4. import java.util.ArrayList;  
  5. import java.util.Arrays;  
  6. import java.util.HashMap;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import java.util.Map.Entry;  
  11.   
  12. import javax.servlet.http.HttpServletRequest;  
  13.   
  14. import org.apache.commons.logging.Log;  
  15. import org.apache.commons.logging.LogFactory;  
  16.   
  17. import com.opensymphony.webwork.RequestUtils;  
  18. import com.opensymphony.webwork.dispatcher.mapper.ActionMapper;  
  19. import com.opensymphony.webwork.dispatcher.mapper.ActionMapping;  
  20. import com.opensymphony.webwork.dispatcher.mapper.RestfulActionMapper;  
  21. import com.opensymphony.xwork.config.ConfigurationManager;  
  22. import com.opensymphony.xwork.config.RuntimeConfiguration;  
  23.   
  24. public class BetterRestfulActionMapper implements ActionMapper {  
  25.       
  26.     protected static final Log LOG = LogFactory.getLog(RestfulActionMapper.class);  
  27.     private static final String ID = "id";   
  28.   
  29.     public ActionMapping getMapping(HttpServletRequest request) {  
  30.         String uri = RequestUtils.getServletPath(request);  
  31.         if (uri.indexOf('.', uri.length() - 5) != -1) {  
  32.             return null;  
  33.         }  
  34.           
  35.         int pos1 = uri.indexOf('/', 1);  
  36.         if (pos1 == -1) {  
  37.             return null;  
  38.         }  
  39.         String part1 = uri.substring(1, pos1);  
  40.           
  41.         String namespace = "";  
  42.         String actionName;  
  43.         int pos2 = uri.indexOf('/', pos1 + 1);  
  44.         if (pos2 == -1) {  
  45.             actionName = part1;  
  46.             uri = uri.substring(pos1 + 1);  
  47.         } else {  
  48.             RuntimeConfiguration config = ConfigurationManager.getConfiguration()  
  49.                 .getRuntimeConfiguration();  
  50.             String part2 = uri.substring(pos1 + 1, pos2);  
  51.             if (config.getActionConfigs().containsKey("/" + part1)) {  
  52.                 namespace = "/" + part1;  
  53.                 actionName = part2;  
  54.                 uri = uri.substring(pos2 + 1);  
  55.             } else {  
  56.                 actionName = part1;  
  57.                 uri = uri.substring(pos1 + 1);  
  58.             }  
  59.         }  
  60.           
  61.         List<String> list = new ArrayList<String>(Arrays.asList(uri.split("/")));  
  62.         if (list.size() % 2 == 1) {  
  63.             list.add(0, ID);  
  64.         }  
  65.         HashMap<String, String> params = new HashMap<String, String>();  
  66.         for (int i = 0; i < list.size() / 2; ++i) {  
  67.             try {  
  68.                 String name = URLDecoder.decode(list.get(2 * i), "UTF-8");  
  69.                 String value = URLDecoder.decode(list.get(2 * i + 1), "UTF-8");  
  70.                 params.put(name, value);  
  71.             } catch (Exception ex) {  
  72.                 LOG.warn(ex);  
  73.             }  
  74.         }  
  75.           
  76.         return new ActionMapping(actionName, namespace, "", params);  
  77.     }  
  78.   
  79.     public String getUriFromActionMapping(ActionMapping mapping) {  
  80.         String uri = mapping.getNamespace() + "/" + mapping.getName();  
  81.         Map params = mapping.getParams();  
  82.         if (params.containsKey(ID)) {  
  83.             uri += "/" + params.get(ID);  
  84.         }  
  85.         for (Object o : mapping.getParams().entrySet()) {  
  86.             Entry entry = (Entry) o;  
  87.             String name = (String) entry.getKey();  
  88.             if (!name.equals(ID)) {  
  89.                 uri += "/" + name + "/" + entry.getValue();  
  90.             }  
  91.         }  
  92.         return uri;  
  93.     }  
  94. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics