我选择springboot 版本 1.4.2.RELEASE这个版本。1.5.x的版本已经不再支持这么干了。
选择了版本还需要将jsp打包到,springboot jar 包的META-INF/resources/目录下。两个方法:
一:
<resources>
<resource>
<!-- 指定resources插件处理哪个目录下的资源文件 -->
<directory>src/main/webapp</directory>
<!--注意此次必须要放在此目录下才能被访问到 -->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
二:
resources目录下新建META-INF/resources文件夹
最后打成包的结构是这样的:
然后就OK了。但是springboot是不推荐jsp的。最好不要用。
华丽的分割线------------------------------------------------------------------------------------------------------------------------------
springboot官网说: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 Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also
be deployable to a standard container (not limited to, but including Tomcat). An executable jar will not
work because of a hard coded file pattern in Tomcat.
那么就是说好像把锅甩给了tomcat。但是呢。我们知道,springboot启动内嵌tomcat的时候是有临时目录的。
那么我暴力一点把jsp文件复制到tomcat目录中可不可以呢?
@Component
public class EmbeddedTomcatJSPExtractor implements ServletContextAware {
private static final Logger LOG = LoggerFactory.getLogger(EmbeddedTomcatJSPExtractor.class);
private static final String TPM_ROOT_PATH = "/";
private static final String PATH_SPLIT = "/";
private final String resourcePathPrefix;
private final String resourceFileSuffix = ".jsp";
private ServletContext servletContext;
public EmbeddedTomcatJSPExtractor(@Value("${spring.mvc.view.prefix:WEB-INF/}") String springMVCPrefix){
this.resourcePathPrefix = springMVCPrefix.startsWith(PATH_SPLIT) ? springMVCPrefix.substring(1) : springMVCPrefix;
}
protected boolean validateJarEntry(JarEntry entry){
return !entry.isDirectory()
&& entry.getName().startsWith(resourcePathPrefix)
&& entry.getName().endsWith(resourceFileSuffix);
}
@PostConstruct
public void extractor() throws IOException {
LOG.info("resource path prefix : {}", resourceFileSuffix);
try {
Object jarFile = getClass().getProtectionDomain().getCodeSource().getLocation().getContent();
LOG.info("Tomcat temp path : " + getTempRootPath());
if (JarFile.class.isInstance(jarFile)) {
processJarFile((JarFile) jarFile);
} else {
LOG.info("is not an executable jar.");
}
} catch (MalformedURLException e) {
LOG.warn("Cannot find jar file : {}", e.getMessage());
} catch (IOException e) {
throw new IOException(e);
}
}
private void processJarFile(JarFile jarFile) throws IOException {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (validateJarEntry(entry)) {
extractFileToTemp(jarFile, entry);
}
}
}
private final String getTempRootPath(){
return servletContext.getRealPath(TPM_ROOT_PATH);
}
private void extractFileToTemp(JarFile jarFile, JarEntry entry) throws IOException {
try (InputStream in = jarFile.getInputStream(entry);
OutputStream out = getTempOutputStream(entry);
){
IOUtils.copy(in, out);
}
}
private FileOutputStream getTempOutputStream(JarEntry entry) throws IOException {
String fileName = getFileName(entry);
String parentRelativeFolderPath = getParentRelativeFolderPath(entry);
File parentFolder = new File(getTempRootPath() + parentRelativeFolderPath);
parentFolder.mkdirs();
File tempFile = new File(parentFolder, fileName);
tempFile.createNewFile();
return new FileOutputStream(tempFile);
}
private String getParentRelativeFolderPath(JarEntry entry) {
return entry.getName().substring(0, entry.getName().lastIndexOf(PATH_SPLIT) + 1);
}
private String getFileName(JarEntry e) {
int index = e.getName().lastIndexOf(PATH_SPLIT);
return e.getName().substring(index == -1 ? 0 : index, e.getName().length());
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
}
OK
参考资料:
https://docs.spring.io/spring-boot/docs/2.0.0.M6/reference/htmlsingle/点击打开链接
https://docs.spring.io/spring-boot/docs/2.0.0.M6/reference/htmlsingle/#boot-features-jsp-limitations点击打开链接
https://stackoverflow.com/questions/21243690/is-it-possible-with-spring-boot-to-serve-up-jsps-with-a-jar-packaging/46057848#46057848点击打开链接