spring boot打包成war包的頁面該放到哪裏?

背景css

常常有朋友問我,平時都是使用spring mvc,打包成war包發佈到tomcat上,如何快速到切換到spring boot的war或者jar包上?html

先來看看傳統的war包樣式是什麼樣子的?java

1. 傳統的spring MVC格式的war包web

spring boot打包成war包的頁面該放到哪裏?

 

能夠看到,webapp/resouces文件存放css/js/html等靜態文件,WEB-INF存放jsp動態文件。spring

對應的配置文件centos

@EnableWebMvc //mvc:annotation-driven
@Configuration
@ComponentScan({ "com.xxx.web" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {
 
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
    
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
 
}

對應xml的配置以下:spring-mvc

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd ">
 
    <context:component-scan base-package="com.xxxx.web" />
 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
 
    <mvc:resources mapping="/resources/**" location="/resources/" />
     
    <mvc:annotation-driven />
 
</beans>

 

2.spring boot格式的jar包tomcat

jar的結構,spring 儘可能避免jsp的動態文件,而是使用如Thymeleaf 、FreeMarker等模板引擎,由於jsp有不少限制。mvc

spring boot打包成war包的頁面該放到哪裏?

 

28.4.5 JSP Limitationsapp

When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.

Undertow does not support JSPs.

Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.

3.spring boot 格式的war包

spring boot打包成war包的頁面該放到哪裏?

 

如何切換?

其實,經過上面的結構,咱們能夠看出,spring boot的標準規格仍是不建議使用jsp的,推薦使用Thymeleaf 、FreeMarker等模板引擎,而後全部的靜態文件一樣存儲在resources下面,能夠使用代碼配置動態代碼

@Configuration
@EnableWebMvc
public class SpringConfig
{
    @Bean
    public InternalResourceViewResolver viewResolver()
    {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        
        return viewResolver;
    }
}

 

或者靜態屬性配置

spring.mvc.static-path-pattern=/resources/**

來自定義配置。

也能夠使用靜態文件動態化

spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
spring.resources.chain.strategy.fixed.enabled=true
spring.resources.chain.strategy.fixed.paths=/js/lib/
spring.resources.chain.strategy.fixed.version=v12

注意:centos下使用tomcat時,編譯的jsp文件,上傳的文件等等默認都存儲在臨時目錄裏,會

If you choose to use Tomcat on centos, be aware that, by default, a temporary directory is used to store compiled JSPs, file uploads, and so on. This directory may be deleted by tmpwatch while your application is running, leading to failures. To avoid this behavior, you may want to customize your tmpwatch configuration such that tomcat.* directories are not deleted or configure server.tomcat.basedir such that embedded Tomcat uses a different location.

參考資料

【1】https://www.mkyong.com/spring-boot/spring-boot-deploy-war-file-to-tomcat/

【2】https://www.baeldung.com/spring-boot-war-tomcat-deploy

【3】https://docs.spring.io/spring-boot/docs/2.1.2.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content

相關文章
相關標籤/搜索