1. 程式人生 > >SpringBoot專案使用外接tomcat

SpringBoot專案使用外接tomcat

我們知道SpringBoot使用的內建預設的tomcat,缺點好像是不支援jsp還是什麼,但是在釋出環節還是要放到tomcat中

第一步修改啟動類
繼承 SpringBootServletInitializer類並且重寫* configure*方法


@SpringBootApplication
public class Application extends SpringBootServletInitializer{

    @Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
        return
application.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

之前的保持不變,然後讓它繼承SpringBootServletInitializer類,新增一個config方法。

更新你的Maven or Gradle 打包方式配置

    <packaging>war</packaging>  
apply plugin: 'war'

確保內嵌的servlet容器不能干擾war包將部署的servlet容器

    <dependencies>  
        <!-- … -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-tomcat</artifactId>  
            <scope>provided</scope
>
</dependency> <!-- … --> </dependencies>
    dependencies {  
        // …  
        providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'  
        // …  
    }  

maven or Gradle 在build的時候就會打成war包,這裡可能還需要注意一個編碼的問題

配置好這些,確實能在Tomcat啟動了,但是對於Controller返回頁面檢視,卻還不夠,還需要配置模板的引數,這裡我使用的是Thymeleaf ,所以就介紹Thymeleaf 的配置方式

# THYMELEAF (ThymeleafAutoConfiguration)  
spring.thymeleaf.check-template-location=true  
spring.thymeleaf.prefix=classpath:/templates/  
spring.thymeleaf.suffix=.html  
spring.thymeleaf.mode=HTML5  
spring.thymeleaf.encoding=UTF-8  
spring.thymeleaf.content-type=text/html  
spring.thymeleaf.cache=false
    spring: 
      thymeleaf: 
        cache: false  
        check-template-location: true  
        prefix: classpath:/templates/  
        suffix: .html  
        mode: HTML5  
        encoding: UTF-8  
        content-type: text/html