html如何转成war文件,spring boot打包成war包的页面如何存放

背景

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

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

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

9b826478100ac201666af77a5faa4481.png

可以看到,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的配置如下:

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

2.spring boot格式的jar包

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

1ca3a9bf674b57c5d713b6ff36753881.png

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包

82d58ce7d3ba90e74570a429eca2ad11.png

如何切换?

其实,通过上面的结构,我们可以看出,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.

总结

以上所述是小编给大家介绍的spring boot打包成war包的页面如何存放,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值