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

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

问题描述

我有一个网络应用,其中有不同的导航锚标签,例如主页、个人资料等.

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

我想要的:

当我按下主页或个人资料等锚点标签时.我只想确保当前用户在该标签/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 页面的上下文.提供对各种对象的访问,包括:
servlet 上下文:...
会话:...
请求:...
回应:...

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.

例如,如果通过 http://myhost.com/myWebApp/thisJSP.jsp 访问您的 JSP(名为 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天全站免登陆