Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to store pages packed with springboot into war packages

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to store the pages packed with springboot into war packages, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Background

My friends often ask me how to quickly switch to spring boot's war or jar package when I usually use spring mvc and package it into a war package and publish it to tomcat.

Let's first take a look at what the traditional war package style looks like.

1. Traditional war package in spring MVC format

As you can see, webapp/resouces files store static files such as css/js/html, and WEB-INF stores jsp dynamic files.

Corresponding configuration file

@ 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;}}

The corresponding xml is configured as follows:

Jar package in 2.spring boot format

The structure of jar, spring try to avoid jsp dynamic files, but use template engines such as Thymeleaf, FreeMarker and so on, because jsp has many limitations.

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.

War package in 3.spring boot format

How to switch?

In fact, through the above structure, we can see that the standard specification of spring boot is not recommended to use jsp, it is recommended to use Thymeleaf, FreeMarker and other template engines, and then all static files are also stored under resources, you can use code to configure dynamic code

@ Configuration@EnableWebMvcpublic class SpringConfig {@ Bean public InternalResourceViewResolver viewResolver () {InternalResourceViewResolver viewResolver = new InternalResourceViewResolver (); viewResolver.setPrefix ("/ WEB-INF/view/"); viewResolver.setSuffix (".jsp"); return viewResolver;}}

Or static property configuration

Spring.mvc.static-path-pattern=/resources/**

Customize the configuration.

You can also use static files to make them dynamic

Spring.resources.chain.strategy.content.enabled=truespring.resources.chain.strategy.content.paths=/**spring.resources.chain.strategy.fixed.enabled=truespring.resources.chain.strategy.fixed.paths=/js/lib/spring.resources.chain.strategy.fixed.version=v12

Note: when using tomcat under centos, compiled jsp files, uploaded files, etc., are stored in a temporary directory by default.

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.

After reading the above, do you have any further understanding of how to store the pages where springboot is packaged into war packages? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report