简体   繁体   English

Spring Boot 上下文路径被外部 Tomcat 忽略

[英]Spring Boot Context Path Ignored With External Tomcat

I have set the context path for tomcat as follows:我已经为 tomcat 设置了上下文路径,如下所示:

@Component
public class CustomContainer implements
        WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.setContextPath("/capripol");
        factory.setPort(8080);
    }
}

Navigating to localhost:8080/capripol works fine and I am prompted with my login screen, however after logging in my forms and controllers do not append to the context path, so instead of navigating to /capripol/MainMenu etc. they navigate to /MainMenu.导航到 localhost:8080/capripol 工作正常,我的登录屏幕提示我,但是在登录我的表单和控制器后不会附加到上下文路径,所以不是导航到 /capripol/MainMenu 等,而是导航到 /MainMenu . How do I set the context path such that my form actions and controllers will be appended do it - why is the tomcat factory context path not setting?我如何设置上下文路径,以便我的表单操作和控制器将被附加这样做 - 为什么 tomcat 工厂上下文路径没有设置?

Edit: My Application class编辑:我的应用程序类

@SpringBootApplication
public class CapripolApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(CapripolApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CapripolApplication.class);
    }

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**")
                    .addResourceLocations("classpath:/static/", "classpath:/images/")
                    .setCachePeriod(0);
        }
    }

}

A few ways to do it.有几种方法可以做到。 You can add it to each controller, usefully if you want to change the context path您可以将它添加到每个控制器,如果您想更改上下文路径,这将非常有用

@Controller
@RequestMapping(value = "/foo")
public class bar{
    @GetMapping(value = "/bar")
    public void stuff(){
        //doing stuff
    }
}

Or you can put it in your application.properties / yml或者你可以把它放在你的 application.properties / yml

server.servlet.contextPath=/foo/*

There are technically some other more round about ways to do it, especially if you are using an older version of Spring, but I would think the application properties is what you are looking for.从技术上讲,还有一些其他更全面的方法可以做到这一点,特别是如果您使用的是旧版本的 Spring,但我认为应用程序属性正是您正在寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM