简体   繁体   English

Spring MVC 上下文路径在哪里设置?

[英]Where is Spring MVC context path set?

I am not sure how context path is set.我不确定如何设置上下文路径。 When I rename my .war file in tomcat on autodeploy , the web page goes to localhost:8080/newDirectory as expected, however for some reason wherever there's a call to pageContext.request.contextPath in a Spring based page, it still returns the old context path.当我在autodeploy上的 tomcat 中重命名我的.war file时,网页按预期转到localhost:8080/newDirectory ,但是由于某种原因,在基于 Spring 的页面中调用pageContext.request.contextPath时,它仍然返回旧的上下文路径。

I tried to override the context path by setting:我试图通过设置来覆盖上下文路径:

<context path="/newDirectory" docBase="appName" override="true"></context>

in server.xml but it doesn't work.server.xml但它不起作用。

My question is, where does Spring read its context path from?我的问题是,Spring 从哪里读取它的上下文路径? I used Maven and I did see there's a我使用了 Maven,我确实看到有一个

<appContext>/${project.artifactId}</appContext>

in the pom.xml , does this mean I need to rename the artifactId to newDirectory ?pom.xml ,这是否意味着我需要将 artifactId 重命名为 newDirectory ?

I have also tried adding that <context path="/newDirectory"...> in /META-INF/context.xml (which now I know will be ignored anyway due to my server.xml changes).我还尝试<context path="/newDirectory"...> in /META-INF/context.xml添加<context path="/newDirectory"...> in /META-INF/context.xml (现在我知道由于我的server.xml更改,它无论如何都会被忽略)。

Thanks in advance for your answer.预先感谢您的回答。

It not depends on Spring maybe you are using a maven plugin to build your war that reads the appContext property.这不取决于 Spring,也许您正在使用 Maven 插件来构建读取 appContext 属性的战争。 You can read about definig ServletContext in this thread.您可以在线程中阅读有关定义 ServletContext 的信息。

Every PageRequest will get current HttpServletRequest object and get context path of current request and append your .jsp (that will work even if the context-path this resource is accessed at changes). 每个PageRequest都将获取当前的HttpServletRequest对象并获取当前请求的上下文路径并附加您的.jsp(即使在更改时访问此资源的上下文路径,它也会起作用)。

Eg, 例如,

if you have war file as MyCompany.war and having a page with ${pageContext.request.contextPath}/MyJspPage.jsp. 如果你有一个war文件作为MyCompany.war并且有一个带有$ {pageContext.request.contextPath} /MyJspPage.jsp的页面。

Then your context path is http://abc/MyCompany and it works as http://abc/MyCompany/MyJspPage.jsp.

Suppose if you change your war file as OurCompany.war, 假设您将war文件更改为OurCompany.war,

then your context path changes to http://abc/OurCompany and Jsp will work as http://abc/OurCompany/MyJspPage.jsp.

It means context path will change automatically to name of the application(War file name) with out any changes. 这意味着上下文路径将自动更改为应用程序的名称(War文件名)而不进行任何更改。

In your case,after renaming war file name with newDirectory,your webserver will deploy newDirectory application but still newDirectory application exist in the web server.I think you should delete old application from webapp and then check by reloading your newDirectory application. 在您的情况下,在使用newDirectory重命名war文件名后,您的Web服务器将部署newDirectory应用程序,但Web服务器中仍然存在newDirectory应用程序。我认为您应该从webapp删除旧应用程序,然后通过重新加载newDirectory应用程序进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM