Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

My question is, where does Spring read its context path from? I used Maven and I did see there's a

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

in the pom.xml, does this mean I need to rename the artifactId to 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).

Thanks in advance for your answer.

like image 375
testing test Avatar asked Jul 02 '15 08:07

testing test


People also ask

How do you find the context path?

The typical way of getting the context path is through the HttpServletRequest class. Simply you can add a HttpServletRequest parameter to your controller method and then get the context path using getContextPath() method. Now that you get the context path, you can pass it to the services that need it.

How do I change the context path in STS?

1.1 Right click on the project, select Properties , Web Project Settings , update the context root here. 1.2 Remove your web app from the server and add it back. The context root should be updated. 1.3 If step 2 is failing, delete the server, create a new server and add back the web app.

What is default context path in Spring boot?

Spring Boot, by default, serves content on the root context path (“/”). While it's usually a good idea to prefer convention over configuration, there are cases when we do want to have a custom path.

What is the default context path?

The default context path is empty. The context path can be changed in many ways. We can set it in the properties file, with the SERVER_SERVLET_CONTEXT_PATH environment variable, with Java System property, or on the command line. On Unix systems, we can change the SERVER_SERVLET_CONTEXT_PATH with the export command.


1 Answers

It not depends on Spring maybe you are using a maven plugin to build your war that reads the appContext property. You can read about definig ServletContext in this thread.

like image 167
bifulcoluigi Avatar answered Oct 11 '22 11:10

bifulcoluigi