【问题标题】:How to rename Spring MVC Web App context root?如何重命名 Spring MVC Web App 上下文根?
【发布时间】:2016-04-24 18:45:06
【问题描述】:

我正在尝试重命名我的 Spring MVC Web 应用程序。 当我运行它时,URL 中有一个旧名称: http://localhost:8080/oldName/

在项目属性>资源我设置路径:/newName 以及在 Web 项目设置中,上下文根:newName

但是没用,我还有http://localhost:8080/oldName/ 如何重命名?

【问题讨论】:

  • 检查如何更改项目的上下文路径,您可以从 POM.xml 中进行。

标签: java spring spring-web contextpath


【解决方案1】:

就我而言,我使用 ECLIPSE 来开发我的项目。

为了在点击“在服务器上运行”后获得所需的url,我还需要修改Tomcat-v9.0下的server.xml的以下行,如下所示:

<Context docBase="newName" path="/newName" reloadable="true" source="org.eclipse.jst.jee.server:newName"/></Host>

附: 我在下面检查过: 在项目属性>资源中,我设置了路径:/newName,还在 Web 项目设置中,上下文根:newName

【讨论】:

    【解决方案2】:

    方法不止一种,具体取决于你是否使用 spring-boot:

    1. 在 application.properties/yml 文件中:

    server.servlet.context-path=/newName

    1. Java 系统属性:

    您还可以在初始化上下文之前将上下文路径设置为 Java 系统属性:

    public static void main(String[] args)
    {
        System.setProperty("server.servlet.context-path", "/newName");
        SpringApplication.run(Application.class, args);
    }
    
    1. 操作系统环境变量:

    Linux:

    导出 SERVER_SERVLET_CONTEXT_PATH=/newName

    窗户:

    设置 SERVER_SERVLET_CONTEXT_PATH=/newName

    上面的环境变量是针对Spring Boot 2.x.x的,如果我们有1.x.x,变量就是SERVER_CONTEXT_PATH。

    1. 命令行参数

    我们也可以通过命令行参数动态设置属性:

    java -jar app.jar --server.servlet.context-path=/newName

    1. 使用 Java 配置

    在 Spring Boot 2 中,我们可以使用 WebServerFactoryCustomizer:

    @Bean
    public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
        return factory -> factory.setContextPath("/newName");
    }
    

    使用 Spring Boot 1,我们可以创建 EmbeddedServletContainerCustomizer 的实例:

    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
        return container -> container.setContextPath("/newName");
    }
    
    1. Eclipse + Maven

      <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <wtpversion>2.0</wtpversion> <wtpContextName>newName</wtpContextName> </configuration> </plugin>

      1. Eclipse + Gradle

      apply plugin: 'java' apply plugin: 'war' apply plugin: 'eclipse-wtp' eclipse { wtp { component { contextPath = 'newName' } } }

    以下链接可能会有所帮助:

    【讨论】:

      猜你喜欢
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 2019-04-23
      • 2015-08-18
      相关资源
      最近更新 更多