前言
之前將傳統(tǒng)的SpringMVC工程改成了Spring Boot并支持JSP谱轨,但不是jar包運行的方式,這次繼續(xù)改造成Jar包方式支持JSP吠谢。
開始創(chuàng)建
pom.xml
<packaging>jar</packaging>
<!-- 增加jsp支持土童,否則會變成直接下載頁面 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
application.properties
server.servlet.context-path=/familydoctor-webapp
server.port=8081
logging.level.root=INFO
# 屏蔽o.a.tomcat.util.scan.StandardJarScanner : Failed to scan錯誤
logging.level.org.apache.tomcat.util.scan.StandardJarScanner=ERROR
# 相對路徑,默認輸出的日志文件名為spring.log
logging.path=log
spring.profiles.active=prod
FamilyDoctorApplication
package com.asiainfo.aigov;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FamilyDoctorApplication {
public static void main(String[] args) {
SpringApplication.run(FamilyDoctorApplication.class, args);
}
}
頁面存放目錄結(jié)構(gòu)
resources下面的META-INF/resources里的文件默認可以直接讀取工坊,無需額外配置ResourceHandler
目錄結(jié)構(gòu)
結(jié)后語
打包后執(zhí)行會報以下的錯誤:
java.io.FileNotFoundException: file:/Users/pany/Documents/workspace/aigov/familydoctor-webapp/target/familydoctor-webapp.jar!/BOOT-INF/classes!/app.properties (No such file or directory)
這是因為使用下面的寫法在IDE里是可以的献汗,但打成JAR包后不行敢订。
String path = this.getClass().getClassLoader().getResource(fileName).getFile();
要改成
Reader reader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(fileName), "UTF-8");
properties.load(reader);
另外,用默認版本的spring-boot-maven-plugin打出來的Jar包訪問JSP會報404錯誤罢吃,用1.4.2.RELEASE
版本就沒問題楚午。