【问题标题】:Access JSP file Inside Fat Jar访问 Fat Jar 中的 JSP 文件
【发布时间】:2019-05-26 15:18:04
【问题描述】:

我有 spring boot 应用程序,它创建了 fat jar 和 fat 的爆炸版本,如下所示,

/META-INF/resources/index.jsp 如下,

Spring Boot Executable jar structure

我尝试访问 application.properties 中的前缀,

spring.mvc.view.prefix = /WEB-INF/resources/ - No luck
spring.mvc.view.prefix = /META-INF/resources/  - No luck
spring.mvc.view.prefix = /resources/ - No luck

那么这个前缀的正确前缀是什么?提前致谢!

【问题讨论】:

    标签: spring-boot maven-plugin


    【解决方案1】:

    你可以这样做:

    ApplicationConfigurerAdapter.java

    @Configuration
    @EnableWebMvc
    public class ApplicationConfigurerAdapter extends WebMvcConfigurerAdapter{
    
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    
        @Bean
        public InternalResourceViewResolver viewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            return resolver;
        }    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
            registry.addResourceHandler("/web-resources/**").addResourceLocations("/resources/").setCachePeriod(300);
        }
    
    }
    

    【讨论】:

    • 谢谢德本德拉。对我来说,如果我在 eclipse 中运行它是有效的,但是如果我创建可执行 jar 就没有运气了。
    • 您是否采用了嵌入式 tomcat ??将您的 Application.java 扩展为公共类 App 扩展 SpringBootServletInitializer{.......}
    • 是的,但是在阅读了几篇文章之后,只有战争格式支持这些东西。我希望 JAR 被创建为 docker 镜像并在 docker 上运行。
    【解决方案2】:

    确保您已将 maven 插件配置为可执行。

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <executable>true</executable>
        </configuration>
    </plugin>
    

    要在 docker 容器中运行这个 war 文件,只需像运行一个 jar 文件一样运行它。在您的 Dockerfile 中,使用您的战争名称:

    ENTRYPOINT exec java -jar /my_project.war
    

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 2020-08-06
      • 2011-07-07
      • 2017-06-18
      • 2021-03-29
      • 2013-12-13
      • 2012-01-05
      • 1970-01-01
      • 2016-08-21
      相关资源
      最近更新 更多