背景

经常有朋友问我,平时都是使用spring mvc,打包成war包发布到tomcat上,如何快速到切换到spring boot的war或者jar包上?

先来看看传统的war包样式是什么样子的?

1. 传统的spring MVC格式的war包

可以看到,webapp/resouces文件存放css/js/html等静态文件,WEB-INF存放jsp动态文件。

对应的配置文件

@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的配置如下:

<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包

jar的结构,spring 尽量避免jsp的动态文件,而是使用如Thymeleaf 、FreeMarker等模板引擎,因为jsp有很多限制。

28.4.5 JSP Limitations

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的标准规格还是不建议使用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

spring boot打包成war包的页面该放到哪里?的更多相关文章

  1. spring boot 打包为war包方法

    刚刚接触spring boot,其快速开发的特性吸引我去研究一下.于是我写了个demo,用spring boot内置的tomcat运行的很好,但是我需要把它部署到外部的tomcat中,于是从网上查找资 ...

  2. spring boot打包为war包,引入外部jar包

    1,在src/main/resource下新建目录jar,将外部jar包放在该目录下 2,在pom.xml中添加依赖 groupId,artifactId,version可随便写 <depend ...

  3. spring boot 打包成jar 包在发布到服务器上

    http://blog.csdn.net/sai739295732/article/details/49444447

  4. spring boot 项目打成war包部署到服务器

    这是spring boot学习的第二篇了,在上一篇已经整合了spring boot项目了,如果还有小伙伴没有看得可以先去看第一篇 基础整合spring boot项目 到这里的小伙伴应该都是会整合基本的 ...

  5. spring boot生成的war包运行时出现java.lang.NullPointerException: null

    最近写了一个数据库同步的程序,见之前的博客,没有用到spring框架来集成,用的时纯Java代码.然后,项目经理要我把程序合到spring boot框架中,因为涉及到多数据源,时间又比较紧,同意我直接 ...

  6. IDEA中将工程打包成war包及部署到Tomcat流程

    工程打包成war包及部署到Tomcat流程 再IDEA开发工具中,将工程打包成war包流程: 父pom里需要移除内置的tomcat <dependency> <groupId> ...

  7. 如何将springboot工程打包成war包并且启动

    将项目打成war包,放入tomcat 的webapps目录下面,启动tomcat,即 可访问. 1.pom.xml配置修改 <packaging>jar</packaging> ...

  8. spring-boot 打包成 war包发布

    1.用maven打包成war包 2.将war包用zip方式打开,删除里面的tomcat-embed相关的4个包,删除spring-boot-tomcat包 3.将删除了tomcat相关嵌入包后的war ...

  9. 将java project打包成jar包,web project 打包成war包的几种演示 此博文包含图片

    转: http://blog.csdn.net/christine_ruan/article/details/7491559 http://developer.51cto.com/art/200907 ...

随机推荐

  1. bat脚本自动安装Jmeter&Jdk

    一句话能解决的事情,绝对不要写一篇文章:一篇文章能解决的事情,绝对不要使用各种工具:一个工具能解决的事情,绝对不要跑东跑西…… 文章主要介绍脚本如何下载.安装.配置Jmeter&Jdk. 不多 ...

  2. (7)Cmake的使用简介

        CMake是一个跨平台的安装(编译)工具,是一个比Make更高级的的编译配置工具,可以根据不同平台.不同编译器,通过编写CmakeLists,可以控制生成的Makefile,从而控制编译过程. ...

  3. A-07 前向分步算法

    目录 前向分步算法 一.前向分步算法引入 二.前向分步算法详解 2.1 加法模型 2.2 加法模型目标函数优化问题 三.前向分步算法流程 3.1 输入 3.2 输出 3.3 流程 更新.更全的< ...

  4. 2.linux系统基础笔记(延时操作、实时系统中的定时器、事件)

    延时操作 延时操作是操作系统中经常遇到的一种情形.延时的原因很多,有的时候是为了等待外设芯片处理结束,有的时候是为了暂时释放cpu的使用权,有的就是为了希望在一段时间获取资源,如果没法在单位时间内获取 ...

  5. OFD电子文档阅读器功能说明(采用WPF开发,永久免费)

    特别说明 ofd阅读器开发语言为c#,具有完全自主产权,没有使用第三方ofd开发包.可以根据你的需求快速定制开发.本阅读器还在开发完善阶段,如有任何问题,可以联系我QQ:13712486.博客:htt ...

  6. GUI tkinter (pack、grid、place)布局篇

    """1.其实我们已经接触过 tkinter 的一种布局,就是 pack 布 局,它非常简单,我们不用做过多的设置,直接使用一个 pack 函数就可以了.2.grid 布 ...

  7. .Net TCP探索(一)——TCP服务端开发(同时监听多个客户端请求)

        最近在园子里看了大神写的(面试官,不要再问我三次握手和四次挥手),忍不住写段程序来测试一番.     在网上找了很多例子,大多只实现了TCP点对点通讯,但实际应用中,一个服务器端口往往要监听多 ...

  8. Redis实现分布式文件夹锁

    缘起 最近做一个项目,类似某度云盘,另外附加定制功能,本人负责云盘相关功能实现,这个项目跟云盘不同的是,以项目为分配权限的单位,同一个项目及子目录所有有权限的用户可以同时操作所有文件,这样就很容易出现 ...

  9. 禅道部署(基于Linux)

    部署步骤: 1. 查看Linux服务器是32位还是64位的 #getconf LONG_BIT 2. 禅道开源版安装包下载 下载站点1:#wget    http://sourceforge.net/ ...

  10. 基于vue组件,发布npm包

    亲测好用,如出错,请留言 1.项目初始化 使用vue脚手架创建,但vuecli太重,我们使用简单的工程脚手架进行处理,输入命令 vue init webpack-simple my-project n ...