关于URL生成以及RestfulActionMapper的有关问题
关于URL生成以及RestfulActionMapper的问题做了几乎一年的Eclipse RCP开发,最近要做一网站项目,我选择webw
关于URL生成以及RestfulActionMapper的问题
做了几乎一年的Eclipse RCP开发,最近要做一网站项目,我选择webwork,这也是我第一次使用webwork。
感觉webwork默认的 dispatcher manager 要求的URL太丑了,多了个.action,看起来很不舒服。我想找一个类似 rails 那样的
找呀找,让我找到 RestfulActionMapper 这个类,符合我的要求。
我在页面中使用 VTL, 没有用 ww 的 标签。这样根据 action 生成 url 怎么办呢? 我不想硬编码。
于是我在一个 BaseAction 中写下以下代码:
java 代码?
- public?abstract?class?BaseAction?extends?ActionSupport?implements?ServletRequestAware?{??
- ??????
- ????protected?HttpServletRequest?request;??
- ????protected?Helper?helper;??
- ??????
- ????public?void?setServletRequest(HttpServletRequest?request)?{??
- ????????this.request?=?request;??
- ????}??
- ??????
- ????public?Helper?getHelper()?{??
- ????????if?(helper?==?null)?{??
- ????????????helper?=?new?Helper();??
- ????????}??
- ????????return?helper;??
- ????}??
- ??????
- ????public?class?Helper?implements?Serializable?{??
- ????????private?static?final?long?serialVersionUID?=?1L;??
- ??
- ????????public?String?url(String?action,?String?params)?{??
- ????????????String[]?parts?=?params.split(",");??
- ????????????Map
- ????????????for?(String?part?:?parts)?{??
- ????????????????String[]?t?=?part.split(":");??
- ????????????????map.put(t[0].trim(),?t[1].trim());??
- ????????????}??
- ??????????????
- ????????????ActionMapper?manager?=?ActionMapperFactory.getMapper();??
- ????????????ActionMapping?mapping?=?manager.getMapping(request);??
- ????????????mapping.setNamespace(request.getContextPath()?+?"/");??
- ????????????mapping.setName(action);??
- ????????????mapping.setParams(map);??
- ????????????return?manager.getUriFromActionMapping(mapping);??
- ????????}??
- ????}??
- }??
我想在我的前台页面,这样产生 url:
xml 代码?
- <!---->??
- ...??
可是,它并没有产生像这样的 url:
xml 代码
- http://.../shops/region/1??
于是我又找呀找,找到了 RestfulActionMapper? 的代码:
竟然是这样:
java 代码?
- ...??
- ????public?String?getUriFromActionMapping(ActionMapping?mapping)?{??
- ????????String?base?=?mapping.getNamespace()?+?mapping.getName();??
- ????????for?(Iterator?iterator?=?mapping.getParams().entrySet().iterator();?iterator.hasNext();)?{??
- ????????????Map.Entry?entry?=?(Map.Entry)?iterator.next();??
- ????????????String?name?=?(String)?entry.getKey();??
- ????????????if?(name.equals(mapping.getName()?+?"Id"))?{??
- ????????????????base?=?base?+?"/"?+?entry.getValue();??
- ????????????????break;??
- ????????????}??
- ????????}??
- ??
- ????????return?base;??
- ????}??
- ...??
小弟首次用 webwork ,比较愚昧
可是不能这样算了(搞不定这些,老板会骂呀), 我写了下面代码:
java 代码?
- public?class?BetterRestfulActionMapper?extends?RestfulActionMapper?{??
- ??
- ????@SuppressWarnings("unchecked")??
- ????@Override??
- ????public?String?getUriFromActionMapping(ActionMapping?mapping)?{??
- ????????String?url?=?mapping.getNamespace()?+?mapping.getName();??
- ????????System.out.println(url);??
- ????????Map
- ????????String?idKey?=?mapping.getName()?+?"Id";??
- ????????String?idValue?=?params.get(idKey);??
- ????????if?(idValue?!=?null)?{??
- ????????????url?=?url?+?"/"?+?idValue;??
- ????????}??
- ????????for?(Entry
- ????????????if?(!entry.getKey().equals(idKey))?{??
- ????????????????url?=?url?+?"/"?+?entry.getKey()?+?"/"?+?entry.getValue();??
- ????????????}??
- ????????}??
- ??????????
- ????????return?url;??
- ????}??
- }??
然后,我修改了 webwork.properties 文件
xml 代码?
- webwork.mapper.class=com.longthsoft.deliver.dispatcher.mapper.BetterRestfulActionMapper??
问题算是解决了, 可是我想知道你们都是怎么办的?
初次用 webwork,还望各位老手多多指教。
小弟成长的路上有你们伴随,幸福无比! 1 楼 bencode 2007-03-07 哈哈,今天使用 ww:form 时,才发现 RestfulActionMapper 没错,是我错了。
于是我删掉了 BetterRestfulActionMapper
又改回了:
webwork.mapper.class=com.opensymphony.webwork.dispatcher.mapper.RestfulActionMapper
然后改写了 url 这个方法,暂时解决了这个问题
2 楼 exo905 2007-06-26 很多开源的URL REWRITE的工具,楼主不用自己写吧 3 楼 bencode 2007-07-11 嘿嘿,开源的找起来麻烦,而且我也算webwork新手,所以研究研究对我也有好处.
见:
http://bencode.iteye.com/admin/show/81622 4 楼 kyvin 2007-07-26 我也是用url rewrite 蛮方便的