【问题标题】:How to set Spring root context path如何设置 Spring 根上下文路径
【发布时间】:2015-09-09 09:17:24
【问题描述】:

我是一个 Spring 新手,正在组合一个 Spring Web 应用程序(不是 Spring-boot - 这有多大区别?)。部署在 Tomcat 7 服务器上。

应用程序已启动并正在运行。我的问题是只能通过标准 URL 访问:

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

以下内容不起作用: http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/

即使我的 web.xml 将 index.html 列为欢迎页面。

该问题的另一个症状是应用程序中的各种链接都需要指定为相对链接,而不是使用前置“/”。

但即使是这些网址也不是我真正想要的。

我宁愿指定

http://mycompany.com:8081/cwing

并让它调出 index.html。

跟随Add context path to Spring Boot application的领导

我在 src/main/resources 中创建了一个 application.xml 文件,内容如下:

server.contextPath=/cwing
server.port=8081

这不起作用。 http://mycompany.com:8081/cwinghttp://mycompany.com:8081/cwing/index.html 一样,在服务器上打开一个空白页面,并出现 404 错误。

我一定错过了什么。还需要什么才能让它按我想要的那样工作?

更新:按照要求,这里是 web.xml(删除了不相关的细节)。

    <?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>

【问题讨论】:

  • 您使用的是哪个服务器?
  • 我正在使用 Tomcat 7 并且已经编辑了原始帖子这样说。
  • 如果您直接部署在tomcat上,它将采用您的pom.xml中指示的版本名称。如果你想在没有它的情况下部署它,请将你的战争名称更改为 ROOT.war,并将战争粘贴到 webapps 文件夹中,删除 ROOT 文件夹,重新启动 tomcat。
  • 战争重命名有什么问题??如果没有,那么据我所知,您将不得不依赖 IDE 将上下文路径设置为 / 或任何您想要的。
  • 我宁愿保留那个版本信息。

标签: spring web-applications contextpath


【解决方案1】:

这与 Spring MVC 无关,而是与特定服务器有关,默认情况下,Tomcat 将您的 WAR 名称作为上下文根。如果您想更改它,您可以随时重命名您的 WAR 文件,或者我建议更改您的 pom.xml 以使用最终名称构建您的 WAR 文件,方法如下:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-18
  • 2019-12-25
  • 2017-04-13
  • 2019-01-20
  • 2015-09-19
  • 2017-04-22
  • 1970-01-01
相关资源
最近更新 更多