`
lluorongsheng
  • 浏览: 33915 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

${pageContext.request.contextPath}的作用

阅读更多
相对路径与绝对路径的作用
一.问题
    JSP中究竟采用绝对路径还是采用相对路径随着所采用技术的越来越复杂,这个问题也变得越来越难以解决。
1)采用相对路径遇到的问题
l         相对路径固然比较灵活,但如果想复制页面内的代码却变得比较困难,因为不同的页面具有不同的相对路径,复制后必须修改每一个连接的路径。
l         如果页面被多于一个的页面所包含,那么被包含页面中的相对路径将是不正确的。
l         如果采用Struts的Action返回页面,那么由于页面路径与Action路径不同,使得浏览器无法正确解释页面中的路径,如页面为/pages /cust/cust.jsp,图片所有目录为/images/title.gif,这时在/pages/cust/cust.jsp中的所用的路径为”../../images/title.gif”,但是如果某一个Action的Forward指向这个JSP文件,而这个Action的路径为 /cust/manage.do,那么页面内容中”../../images/title.gif”就不再指向正确的路径了。
解决以上问题似乎只有使用绝对路径了。
2)采用绝对路径遇到的问题
l         随着不同的Web应用发布方式,绝对路径的值也不同。如Web应用发布为MyApp,则路径”/MyApp/images/title.gif”是正确的,但发布为另一应用时如MyApp2,这个路径就不对了,也许这个情况比较少,但以default方式发布Web应用时以上绝对路径也不同:”/images/title.gif”。
 

二.解决方案
1)采用绝对路径,但为了解决不同部署方式的差别,在所有非struts标签的路径前加${pageContext.request.contextPath},如原路径为:
”/images/title.gif”,改为
“${pageContext.request.contextPath}/images/title.gif”。
代码” ${pageContext.request.contextPath}”的作用是取出部署的应用程序名,这样不管如何部署,所用路径都是正确的。
缺点:
操作不便,其他工具无法正确解释${pageContext.request.contextPath}
2) 采用相对路径,在每个JSP文件中加入base标签,如:
<base href="http://${header['host']}${pageContext.request.contextPath}/pages/cust/relation.jsp" />
这样所有的路径都可以使用相对路径。
缺点:
对于被包含的文件依然无效。
    真正使用时需要灵活应用1)和2),写出更加健壮的代码。
分享到:
评论

相关推荐

    jsp页面中${pageContext.request.contextPath}介绍

    ${pageContext.request.contextPath}是JSP取得绝对路径的方法 例如:浏览器地址栏为”http://localhost:8080/test/index.jsp “ 那么:取出来的就是”http://localhost:8080/test”,简写为”/test” /test 等价于 ...

    JavaScript中${pageContext.request.contextPath}取值问题及解决方案

    在JSP里取${pageContext.request.contextPath},方式一是可以正常取到,但方式二却取到的是 字符“${pageContext.request.contextPath}” 方式一: [removed] var t = "${pageContext.request.contextPath}"; ...

    java web 登录验证码生成类

    ${pageContext.request.contextPath}/checkcode.jsp"/&gt; 另外在web.xml中配置验证码类的加载: &lt;servlet-name&gt;CheckCodeImage &lt;servlet-class&gt;com.netshop.common.util.CheckCodeImage&lt;/servlet-class&gt; ...

    jsp+mysql师生作业互动系统

    &lt;li&gt;&lt;a href="${pageContext.request.contextPath}/student!list"&gt;学生信息管理&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="${pageContext.request.contextPath}/exam!list"&gt;学生作业情况查询&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="${pageContext....

    jsp运行时的问题集合

    自己在实际开发中,发现并解决问题的总结。

    人脸识别研究综述

    人脸识别,用于进行人脸检测.二、javaEE框架学习笔记 2、的涵义: ...其中:“${pageContext.request.contextPath}”就是获取request的绝对路径,它的作用相当于“&lt;%=pageContext.request.contextPath%&gt;” 3、

    springMVC+mybatis

    ${pageContext.request.contextPath}/my/login" method="post"&gt; ${message } 邮箱: 密码: 登录"&gt; ${pageContext.request.contextPath}/my/regist"&gt;注册 ${pageContext.request....

    基于iframe实现类似于ajax的页面无刷新

    form id=”form0″ action=”${pageContext.request.contextPath}/news/baikeAdd.form” enctype=”multipart/form-data” method=”POST” target=”hidden_frame”&gt; … … &lt;/form&gt; &lt;iframe name=’...

    struts2<s:if>使用心得

    href="${pageContext.request.contextPath}/ga/changeTaskAssigment.action?taskInstanceId=&lt;s:property value="id"/&gt;" &gt;我来处理)&lt;/s:if&gt; 受评对象准备评级资料"}'&gt; ${pageContext.request.contextPath}/ga/...

    ext时间控件,可以选择时分秒

    link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/extjs/pulgins/datetime/datetime.css" /&gt; [removed][removed] [removed][removed] [removed][removed] [removed]...

    JSTL高级用法

    ${pageContext.request.contextPath} 服务的web application 的名称 ${pageContext.request.method} 取得HTTP 的方法(GET、POST) ${pageContext.request.protocol} 取得使用的协议(HTTP/1.1、HTTP/1.0) ${...

    kindEditor中关于上传处理

    link rel=”stylesheet” href=”${pageContext.request.contextPath}/common/kingeditor/themes/default/default.css”/&gt; &lt;link rel=”stylesheet” href=”${pageContext.request.contextPath}/common/...

    flash-swf格式计算器使用Object控件嵌入html即可使用(文件内有dom)

    codebase="${pageContext.request.contextPath }/common/plugins/calc/swflash.cab#version=6,0,0,0" id="BAPPFlash116526140886070277838"&gt; value="${pageContext.request.contextPath }/common/plugins/calc...

    轮播jar包

    link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/login/login.css?version=20200311" /&gt; &lt;/head&gt; &lt;body&gt; &lt;li&gt;&lt;img src = "${pageContext....

    CONVERT函数方法大全_JAIMY.files ajax test url

    url:"${pageContext.request.contextPath}/module/updateModuleOrder.html", dateType:"json", data:"moduleId="+id+"&num="+demo1.value, type:"post", success:function(result,textStatus){ ...

    jQuery实现标签子元素的添加和赋值方法

    一、在jsp页面中定义&lt;select&gt;标签,如下: 科室: &lt;select class="dept-name-show" style="width: 70%;...var url = "${pageContext.request.contextPath}/getDepts.do";//请求路径 $(".dept-name-show").cli

    layui实现数据表格隐藏列的示例

    ,url:'${pageContext.request.contextPath}/findcustomers' ,cols: [[ {align:'center', title: '编号', sort: true,type:'numbers',width:100} ]] ,page: true }); 我们就会获得一列有序的数列(ps:这里的...

    基于java的企业级应用开发:简单绑定.ppt

    //...省略向用户注册页面跳转方法 @RequestMapping("/registerUser") public String registerUser(User user) { String username = user.getUsername();... } &lt;body&gt; ${pageContext.request.contextPath }/reg

    HTML+CSS实现动态背景登录页面

    主要介绍了HTML+CSS实现动态背景登录页面的相关资料,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics