pageContext.request.contextPath and generic linking

时间秒杀一切 提交于 2019-12-25 07:27:05

问题


In reference to my older unanswered question, I have a further error / problem that is partially related to it.

generic linking, variables and paths in jsp

When I include my header.jsp like this:

<%@include file="/WEB-INF/view/jsp/common/header.jsp" %>

It works fine.

But does not if I do it like this:

<%@include file="${pageContext.request.contextPath}/view/jsp/common/header.jsp" %>

Error:

HTTP Status 500 - /WEB-INF/view/jsp/common/login/login.jsp (line: 8, column: 1) File "${pageContext.request.contextPath}/view/jsp/common/header.jsp" not found

The above with ${} is a proper way and thats what I had been doing in the past until I started using spring and spring security.

But I dont think its a problem of spring or spring security.

I really cant understand why WEB-INF has a weightage and How can I make my links generic (ref my old stated question)


回答1:


Actually, it's not the proper way in this case. The context path is not used for creating paths within the internals of your app, as in this case. Instead, it is used to generate URLs within the view layer, such as links, form URLs, and etc.

In this case, the following is correct:

<%@include file="/WEB-INF/view/jsp/common/header.jsp" %>

The include line above is referring to a resource that is packaged inside your war file. How would the context path, which is configured at the servlet container, have anything to do with the location of something packaged inside your war?

To recap, you only need to use ${pageContext.request.contextPath} to prefix URLs that are going to be placed in output to the client.



来源:https://stackoverflow.com/questions/18038955/pagecontext-request-contextpath-and-generic-linking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!