这个表达式语言$ {pageContext.request.contextPath}在JSP EL中的作用是什么? [英] What does this expression language ${pageContext.request.contextPath} exactly do in JSP EL?

查看:453
本文介绍了这个表达式语言$ {pageContext.request.contextPath}在JSP EL中的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络应用,我有不同的导航锚标签,如家庭,个人资料等。

I have a web app, where I have different navigation anchor tags such as Home, Profile and etc.

我想要的是什么:

当我按下主页或个人资料等锚标签时。我只想确保当前用户在Tags / JSP页面中获取其信息。

When I press anchor tags like home or profile. I just want to ensure that current user gets its information in that Tags/JSP Page.

我正在尝试的示例:

<a  href="${pageContext.request.contextPath}/JSPAddress.jsp">Profile</a>


推荐答案

pageContext 是JSP中可用的隐式对象。 EL文档

The pageContext is an implicit object available in JSPs. The EL documentation says


JSP页面的上下文。提供对各种对象的访问,包括:

servletContext:...

session:... $
request:... $
响应:...

The context for the JSP page. Provides access to various objects including:
servletContext: ...
session: ...
request: ...
response: ...

因此这个表达式将得到当前的 HttpServletRequest 对象并获取当前请求的上下文路径 /JSPAddress.jsp 附加到它以创建一个链接(即使在更改时访问此资源的上下文路径,它也会起作用)。

Thus this expression will get the current HttpServletRequest object and get the context path for the current request and append /JSPAddress.jsp to it to create a link (that will work even if the context-path this resource is accessed at changes).

此表达式的主要目的是使链接与应用程序上下文保持相对,并使它们与应用程序路径的更改隔离开来。

The primary purpose of this expression would be to keep your links 'relative' to the application context and insulate them from changes to the application path.

例如,如果你的JSP(名为 thisJSP.jsp )在 http://myhost.com/myWebApp/thisJSP.jsp 中访问,上下文路径为 myWebApp 。因此,生成的链接href将是 /myWebApp/JSPAddress.jsp

For example, if your JSP (named thisJSP.jsp) is accessed at http://myhost.com/myWebApp/thisJSP.jsp, thecontext path will be myWebApp. Thus, the link href generated will be /myWebApp/JSPAddress.jsp.

如果有一天,您决定在另一台服务器上部署JSP,其上下文路径为 corpWebApp ,则href为链接生成的内容将自动更改为 /corpWebApp/JSPAddress.jsp ,无需您做任何工作。

If someday, you decide to deploy the JSP on another server with the context-path of corpWebApp, the href generated for the link will automatically change to /corpWebApp/JSPAddress.jsp without any work on your part.

这篇关于这个表达式语言$ {pageContext.request.contextPath}在JSP EL中的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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