如何设置 Spring 根上下文路径 [英] How to set Spring root context path

查看:53
本文介绍了如何设置 Spring 根上下文路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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-快照/index.html

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

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

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.

我宁愿指定

http://mycompany.com:8081/cwing

让它调出 index.html.

and have it bring up the index.html.

遵循向Spring Boot应用程序添加上下文路径

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

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

server.contextPath=/cwing
server.port=8081

这不起作用.http://mycompany.com:8081/cwing 显示一个空白页面,显示 404 错误服务器,http://mycompany.com:8081/cwing/index.html.

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?

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

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>

推荐答案

这与 Spring MVC 无关,而是与特定服务器相关,默认情况下 Tomcat 将您的 WAR 名称作为上下文根.如果你想改变这个,你总是可以重命名你的 WAR 文件,或者我建议改变你的 pom.xml 以使用最终名称构建你的 WAR 文件,这是如何做到的:

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>

这篇关于如何设置 Spring 根上下文路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆