相对路径与绝对路径的作用
一.问题
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),写出更加健壮的代码。
分享到:
相关推荐
${pageContext.request.contextPath}是JSP取得绝对路径的方法 例如:浏览器地址栏为”http://localhost:8080/test/index.jsp “ 那么:取出来的就是”http://localhost:8080/test”,简写为”/test” /test 等价于 ...
在JSP里取${pageContext.request.contextPath},方式一是可以正常取到,但方式二却取到的是 字符“${pageContext.request.contextPath}” 方式一: [removed] var t = "${pageContext.request.contextPath}"; ...
${pageContext.request.contextPath}/checkcode.jsp"/> 另外在web.xml中配置验证码类的加载: <servlet-name>CheckCodeImage <servlet-class>com.netshop.common.util.CheckCodeImage</servlet-class> ...
<li><a href="${pageContext.request.contextPath}/student!list">学生信息管理</a></li> <li><a href="${pageContext.request.contextPath}/exam!list">学生作业情况查询</a></li> <li><a href="${pageContext....
自己在实际开发中,发现并解决问题的总结。
人脸识别,用于进行人脸检测.二、javaEE框架学习笔记 2、的涵义: ...其中:“${pageContext.request.contextPath}”就是获取request的绝对路径,它的作用相当于“<%=pageContext.request.contextPath%>” 3、
${pageContext.request.contextPath}/my/login" method="post"> ${message } 邮箱: 密码: 登录"> ${pageContext.request.contextPath}/my/regist">注册 ${pageContext.request....
form id=”form0″ action=”${pageContext.request.contextPath}/news/baikeAdd.form” enctype=”multipart/form-data” method=”POST” target=”hidden_frame”> … … </form> <iframe name=’...
href="${pageContext.request.contextPath}/ga/changeTaskAssigment.action?taskInstanceId=<s:property value="id"/>" >我来处理)</s:if> 受评对象准备评级资料"}'> ${pageContext.request.contextPath}/ga/...
link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/extjs/pulgins/datetime/datetime.css" /> [removed][removed] [removed][removed] [removed][removed] [removed]...
${pageContext.request.contextPath} 服务的web application 的名称 ${pageContext.request.method} 取得HTTP 的方法(GET、POST) ${pageContext.request.protocol} 取得使用的协议(HTTP/1.1、HTTP/1.0) ${...
link rel=”stylesheet” href=”${pageContext.request.contextPath}/common/kingeditor/themes/default/default.css”/> <link rel=”stylesheet” href=”${pageContext.request.contextPath}/common/...
codebase="${pageContext.request.contextPath }/common/plugins/calc/swflash.cab#version=6,0,0,0" id="BAPPFlash116526140886070277838"> value="${pageContext.request.contextPath }/common/plugins/calc...
link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/login/login.css?version=20200311" /> </head> <body> <li><img src = "${pageContext....
url:"${pageContext.request.contextPath}/module/updateModuleOrder.html", dateType:"json", data:"moduleId="+id+"&num="+demo1.value, type:"post", success:function(result,textStatus){ ...
一、在jsp页面中定义<select>标签,如下: 科室: <select class="dept-name-show" style="width: 70%;...var url = "${pageContext.request.contextPath}/getDepts.do";//请求路径 $(".dept-name-show").cli
,url:'${pageContext.request.contextPath}/findcustomers' ,cols: [[ {align:'center', title: '编号', sort: true,type:'numbers',width:100} ]] ,page: true }); 我们就会获得一列有序的数列(ps:这里的...
//...省略向用户注册页面跳转方法 @RequestMapping("/registerUser") public String registerUser(User user) { String username = user.getUsername();... } <body> ${pageContext.request.contextPath }/reg
主要介绍了HTML+CSS实现动态背景登录页面的相关资料,需要的朋友可以参考下