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 add a webapp folder for springboot

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

Share

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

The content of this article is mainly about how to add a webapp folder to springboot. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

Springboot add webapp folder

The spring boot framework itself does not have a webapp folder, so if you want to achieve no separation between the front and background, you need to manually create a webapp asking price folder.

This is a newly created spring boot project. Next we manually create a webapp for the project using idea.

Create an empty webapp folder here first

Next, click ProjectStructure, that is, the above two screenshots are fine.

Click modules, click the + sign at the top right, find web, and click

This is what happens after web is added. We can click on the small pen on the right to edit the wenapp directory and locate it all under our new webapp folder.

If you find this little blue dot after coming out, it will be a success.

Step on the pit: add webapp folder to access jsp but cannot find static resources

This project has a whim to use springboot, but after entering the pit, I found that there are many holes.

Project description: springboot version is 2.2.2 JDK is 1.8

Springboot officially recommends using the thymeleaf template engine to put static resources in the static under resources, and then put the page in templement. But this time, because time is tight, I want to move the webapp in the previous project directly, and the jsp in it is used directly. As a result, jsp can access it, but all the images in it are reported by css,js.

Directory structure

Create a webapp directory directly at the same level as java, and then copy the webapp from the old spring MVC project directly.

As a result, the jsp in the access can be accessed normally, but none of the static resources can be found.

If you visit css alone, you will jump to the error page.

I know the reason. Springboot should intercept all uri for various processing by default, and then block static resources. If you don't want to block it, you have to configure and release it.

Then Baidu has a lot of methods, but it may be that the springboot version is different or the jdk version is different. Anyway, it cannot be implemented, and the prop file configuration does not take effect. Finally, it is not easy to find a method that can be used in my version:

The paths to static resources scanned by springboot by default are these

Classpath:/static

Classpath:/public

Classpath:/resources

Classpath:/META-INF/resources

Our goal is to compile the content in webapp into the last classpath:/META-INF/resources, and then let this path go.

Solution method

1. Add this to build- > resources in pom.xml to compile webapp into META-INF/resources

Src/main/webapp META-INF/resources * / * *

After this recompilation, you can see that webapp has been compiled into it.

two。 Add a configuration class

Import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.*; @ Configurationpublic class staticConfig implements WebMvcConfigurer {/ / whether this is useless / * @ Override public void addInterceptors (InterceptorRegistry registry) {registry.addInterceptor (new LoginInterceptor ()) .origindePathPatterns ("/ META-INF/resources*") } * / @ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ * *") .addResourceLocations ("classpath:/META-INF/resources/");}}

With this visit, my css,js has finally come out!

If there is still a problem, you need to check whether the return in the interceptor has become false. If the returned result is not 404, but 500 or something, you need to check whether the character set encoding conversion is set. One of the reasons I can't find is the character set conversion.

Ps: although springboot released static resource interception, static resources were intercepted by the interceptor after I added login interceptor.

So you have to specify the release of related files and add excludePathPatters.

Package com.bomc.enterprise.config; import com.bomc.enterprise.interceptor.LoginInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.*; @ Configurationpublic class WebConfig implements WebMvcConfigurer {/ / do not register this in the filter service will report null @ Bean public LoginInterceptor loginInterceptor () {return new LoginInterceptor () } / / add interceptor method @ Override public void addInterceptors (InterceptorRegistry registry) {/ / add interceptor path String [] addPathPatters= {"/ *"} / / add unintercepted paths String [] excludePathPatters= {"/", "/ login/login", "/ login/loginPage", "/ login/register", "/ * * / * .css", "/ * * / * .js", "/ * * / * .png", "/ * * / * .jpg", "/ * * / * .jpeg" "/ * * / * .gif", "/ * * / fonts/*", "/ * * / * .svg", "/ * * / * .ttf", "/ * * / * .woff", "/ * * / * .eot", "/ * * / * .otf", "/ * * / * .woff2"} / / register the login interceptor registry.addInterceptor (loginInterceptor ()) .addPathPatterns (addPathPatters) .intercepdePathPatterns (excludePathPatters); / / add multiple} / / add release static resources @ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ * *") .addResourceLocations ("classpath:/META-INF/resources/") if there are multiple interceptors. }} Thank you for your reading. I believe you have some understanding of "how to add webapp folders to springboot". Go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!

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