如何使用“<%= request.getContextPath()%>"比"../"更好 [英] How is using "<%=request.getContextPath()%>" better than "../"

查看:127
本文介绍了如何使用“<%= request.getContextPath()%>"比"../"更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了许多J2EE项目,其中视图层是JSP.在大多数项目中,我已经看到我们使用scriptlet中的contextPath引用了外部资源,即图像,javascript,jsp,css等.

I have worked on number of J2EE projects where the view layer is JSP. In most projects, I have seen that we reference external resources i.e. images, javascript, jsp's, css etc. using the contextPath in the scriptlet.

代码如下,

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>GC Demo Using HandlebarsJS</title>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jqueryUI-AutoComplete/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/handlebarsJS/handlebars.js"></script>
    <link rel="stylesheet" type="text/css" href="${pageContext.servletContext.contextPath}/js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.css">

从上面的jsp中,我要在同一项目包中(即在我的战争中)导入外部资源.

From the above jsp, here I am importing the external resources which are in my same project bundle i.e. in my war.

现在可以像下面的代码一样编写上面的JSP,

Now the same above JSP can be written as below code,

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>GC Demo Using HandlebarsJS</title>
    <script type="text/javascript" src="../js/jqueryUI-AutoComplete/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="../js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.js"></script>
    <script type="text/javascript" src="../js/handlebarsJS/handlebars.js"></script>
    <link rel="stylesheet" type="text/css" href="../js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.css">

在第二个示例中,我也引用了战争中存在的资源.

Here in the second example too I am referencing the resources present in my war.

现在考虑以上两种情况,第一种情况作为最佳实践具有更大的意义.

Now considering both of the above two cases, the first case is given more significance as a best practise.

为什么?

使用第二种情况有什么弊端?

and what are the drawbacks of using the second case?

使用第二种情况,我们的项目与contextpath紧密结合了吗?

Does using the second case, our project gets more tightly coupled with the contextpath?

请向我解释.

推荐答案

request.getContextPath()-返回应用程序的根路径,而 ../-返回文件的父目录.

request.getContextPath()- returns root path of your application, while ../ - returns parent directory of a file.

您使用request.getContextPath(),因为它始终指向应用程序的根目录.如果要将jsp文件从一个目录移动到另一个目录,则无需进行任何更改. 现在,考虑第二种方法.如果要将jsp文件从一个文件夹移动到另一个文件夹,则必须在引用文件的每个位置进行更改.

You use request.getContextPath(), as it will always points to root of your application. If you were to move your jsp file from one directory to another, nothing needs to be changed. Now, consider the second approach. If you were to move your jsp files from one folder to another, you'd have to make changes at every location where you are referring your files.

另外,使用request.getContextPath()的更好方法是在变量中设置'request.getContextPath()',然后使用该变量来引用您的路径.

Also, better approach of using request.getContextPath() will be to set 'request.getContextPath()' in a variable and use that variable for referring your path.

<c:set var="context" value="${pageContext.request.contextPath}" />
<script src="${context}/themes/js/jquery.js"></script>

PS-这是我能弄清楚的原因之一.不知道它是否还有其他意义.

PS- This is the one reason I can figure out. Don't know if there is any more significance to it.

这篇关于如何使用“&lt;%= request.getContextPath()%&gt;"比"../"更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆