-->

How to set Spring root context path

2020-04-15 11:13发布

问题:

I am a Spring newbie and am putting together a Spring web-app (not Spring-boot - how much difference does this make?). Deployment is on a Tomcat 7 server.

The app is up and running. My problem is that is only accessible via the standard URL:

http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/index.html

The following do not work: http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/

even though my web.xml lists index.html as the welcome page.

An additional symptom of the problem is that all sorts of links within the application need to be specified as relative, rather than with a prepended '/'.

But even these urls are not what I really want.

I would rather specify

http://mycompany.com:8081/cwing

and have it bring up the index.html.

Following the lead of Add context path to Spring Boot application

I created an application.xml file in src/main/resources with the following content:

server.contextPath=/cwing
server.port=8081

This doesn't work. http://mycompany.com:8081/cwing brings up a blank page, with a 404 error on the server, as does http://mycompany.com:8081/cwing/index.html.

I must be missing something. What else is needed to get this to work as I want it to work?

UPDATE: as asked, here is the web.xml (irrelevant details removed).

    <?xml version="1.0"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
              version="3.0">
      <display-name>cwing</display-name>

        <!-- Creates the Spring Container shared by all Servlets and Filters -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- The definition of the Root Spring Container shared by all Servlets 
            and Filters -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            /WEB-INF/spring/context.xml
            /WEB-INF/spring/datasource.xml
            /WEB-INF/spring/security.xml
            </param-value>
        </context-param>

        <servlet>
            <servlet-name>d</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    /WEB-INF/spring/context.xml
                    /WEB-INF/spring/datasource.xml
                    /WEB-INF/spring/security.xml
                </param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
            <async-supported>true</async-supported>
        </servlet>

        <servlet-mapping>
            <servlet-name>d</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>

        <!-- Disables Servlet Container welcome file handling. Needed for compatibility 
            with Servlet 3.0 and Tomcat 7.0 -->
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>

        <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        </filter>

        <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping> 
...
</web-app>

回答1:

This is not related to Spring MVC but to specific server, by default Tomcat gets your WAR name as context root. If you want to change this, you can always rename your WAR file or I recommends to change your pom.xml to build your WAR file with final name, here is how to do this:

<build>
  <finalName>finalName</finalName>
 . . .
</build>