欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Url映射类的实现,Url映射类实现,package com.

来源: javaer 分享于  点击 18313 次 点评:124

Url映射类的实现,Url映射类实现,package com.


package com.lovver.jrails.rest;import javax.servlet.http.HttpServletRequest;import org.apache.commons.lang.ArrayUtils;import org.apache.commons.lang.StringUtils;import org.apache.commons.lang.WordUtils;import com.lovver.jrails.common.config.JRailsConfig;public class JRailsRouter {private static JRailsRouter jRailsRouter = null;public static JRailsRouter getInstants() {if (jRailsRouter == null) {jRailsRouter = new JRailsRouter();}return jRailsRouter;}public PathMapping getPathMapping(HttpServletRequest request,JRailsConfig jRailsConfig) {PathMapping pathMapping = new PathMapping();/**去掉ContextPath*/String contextPath=request.getContextPath();String pathInfo = request.getRequestURI();if(jRailsConfig.getJRailsServletConfig().isDebugMode()){System.out.println("requestURI="+pathInfo);}pathInfo = StringUtils.replace(pathInfo, contextPath, "");/**去掉url的servlet映射后缀*/String urlPrefix=jRailsConfig.getUrlPrefix();urlPrefix=urlPrefix.substring(1);pathInfo = StringUtils.replace(pathInfo, urlPrefix, "");/**开始进行url映射*/String arrPathMapping[] = {};if (StringUtils.isNotEmpty(pathInfo)) {if (pathInfo.length() > 1) {// pathInfo = "/controller/action/id"arrPathMapping = pathInfo.substring(1).split("/");}}/**url存在参数的场合*/String params[]={};if (arrPathMapping.length > 2) {params=new String[arrPathMapping.length -2];for(int i=2;iparams[i-2]=arrPathMapping[i];}}pathMapping.set_params(params);/**url不存在参数的场合*///只有ControllerString _controller = "";String _action = "index";try{_controller = this.normalizeName(arrPathMapping[0])+"Controller";}catch(Exception e){}try{try{// pathInfo = "/controller/id"Integer.parseInt(arrPathMapping[1]);_action = "index";params=(String[]) ArrayUtils.addAll(new String[]{arrPathMapping[1]}, params);pathMapping.set_params(params);}catch(Exception e){_action = arrPathMapping[1];}}catch(Exception e){}pathMapping.set_controller(_controller);pathMapping.set_action(_action);return pathMapping;}/*** This method will change the uri name to some other way. You may override* this function to supply another translate method. By default, this method* will translate the uri to capitalize way and replace the '.', ' ', '-'* and '_' to empty string and cpaitalize the next character. So, the uri* like /index.html will be translate to IndexHtml, login-page will be* LoginPage* * @param uri* @return*/protected String normalizeName(String uri) {char[] cap = { '.', ' ', '-', '_' };return WordUtils.capitalizeFully(uri, cap).replaceAll("\\\\.", "").replaceAll(" ", "").replaceAll("-", "").replaceAll("_", "");}public class PathMapping {private String _controller = "";private String _action = "index";private String _params[] = {};public String get_controller() {return _controller;}public String[] get_params() {return _params;}public void set_params(String[] _params) {this._params = _params;}public void set_controller(String _controller) {this._controller = _controller;}public String get_action() {return _action;}public void set_action(String _action) {this._action = _action;}}}//该片段来自于http://byrx.net
相关栏目:

用户点评