`

【转】关于web路径的获取方法

    博客分类:
  • JDBC
阅读更多

假定你的web application 名称为news,你在浏览器中输入请求路径:  
http://localhost:8080/news/main/list.jsp  
则执行下面向行代码后打印出如下结果:  
1、 System.out.println(request.getContextPath()); //可返回站点的根路径。也就是项目的名字  
打印结果:/news  
2、System.out.println(request.getServletPath());  
打印结果:/main/list.jsp  
3、 System.out.println(request.getRequestURI());  
打印结果:/news/main/list.jsp  
4、 System.out.println(request.getRealPath("/"));  

打印结果:F:\Tomcat 6.0\webapps\news\test 

结合到Spring MVC项目来说一下:

1.jsp:           

 <li><a class="add" rel="add-category" href="${ctx}/admin/category/create.do?parentId=${param.q_eq_parentId}" target="navTab" title="添加目录"><span>添加</span></a></li>

2.如何找到${ctx}/admin/category/create.do地址?

首先,找ctx代表什么。假如:pageContext.setAttribute("ctx", application.getContextPath());从上面的解说可以知道System.out.println(request.getContextPath())得出的是/news。故完整路径为:/news/admin/category/create.do。至于参数则根据实际研究。

 

内容来源:

http://my.oschina.net/xsh1208/blog/177522

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics