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

Can SpringBoot support multiple view parsers jsp+html+ other template engines at the same time

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail whether SpringBoot can support multiple view parsers jsp+html+ and other template engines at the same time, the content of the article is of high quality, so the editor shares it for you to do a reference. I hope you can have a certain understanding of the relevant knowledge after reading this article.

SpringBoot supports multiple view parsers jsp+html+ and other template engines at the same time!

There is a not-so-old project that has gone through several waves of human iterations and the source code maintenance is a mess. View this part, use jsp,html,freemarker and so on. The view is not uniform, so that when html is enabled, jsp and freemarker cannot be accessed. These mutually exclusive factors caused the project to run 3, through the Nginx to adapt.

Today, I share with you a way to make the SpringBoot project support multiple template engines at the same time.

First of all, there are no new technologies, new inventions. It's just a slight modification based on the principle of the WebMvc view parser.

As we all know, there are three main classes in SpringMVC: DispatherServlet (front-end controller), ViewResolver (view parser), and View (view class).

The following is a simple flow of view parsing.

As you can see from the figure, if we want to support multiple views, we need to configure multiple view parsers. For example: ThymeleafViewResolver, InternalResourceViewResolver (system default implementation), ContentNegotiatingViewResolver, BeanNameViewResolver and so on.

When selecting a view parser, DispatcherServlet uses priority as the processing principle. This priority is based on the ViewResolver to implement the Ordered interface or use @ Order annotation to assign values. The higher the minimum priority of the number, the higher the priority.

The corresponding view parser then returns a concrete View class. Finally, the view content such as HTML or XML is rendered through 3 or 4 steps.

The following figure shows the specific sorting method. ViewResolvers is a collection of List.

After sorting, matching the method of the View object, traversing the viewResolvers, matching to the first View object, it is returned.

Therefore, some 404 occurs when multiple views are configured and multiple views need to be supported at the same time (when we configure multiple view parsers, only one view parser is supported, and the other types generate 404).

Take ThymeleafViewResolver as an example. In ThymeleafAutoConfiguration, you can see that its Order is Ordered.LOWEST_PRECEDENCE-5.

The InternalResourceViewResolver automatically injected by SpringBoot has the lowest priority.

Note: this refers to automatic injection, which is the lowest level without any modification.

@ Bean

Public InternalResourceViewResolver viewResolver () {

InternalResourceViewResolver viewResolver = new InternalResourceViewResolver ()

ViewResolver.setPrefix ("/ WEB-INF/")

ViewResolver.setSuffix (".jsp")

ViewResolver.setOrder (0)

ViewResolver.setContentType ("text/html;charset=UTF-8")

Return viewResolver

}

Because a match is made to a View object, it is returned immediately. As a result, no matter how many view parsers you configure, the View returned may not be correct, which leads to the emergence of 404.

Then whether it is possible to dynamically adjust the sorting, or dynamically specify the view parser becomes the key. Fortunately, SpringMVC left us a gap. We just need to rewrite an InternalResourceView from time to time.

Public class HandleResourceViewExists extends InternalResourceView {

@ Override

Public boolean checkResource (Locale locale) {

File file = new File (this.getServletContext () .getRealPath ("/") + getUrl ())

/ / determine whether the page exists

Return file.exists ()

}

}

Through the above code, let's first check whether the corresponding template file exists. Then adjust the view parser dynamically through the following code.

@ Bean

Public InternalResourceViewResolver htmlViewResolver () {

InternalResourceViewResolver viewResolver = new InternalResourceViewResolver ()

ViewResolver.setPrefix (/)

/ / set the inspector

ViewResolver.setViewClass (HandleResourceViewExists.class)

ViewResolver.setSuffix (".html")

ViewResolver.setOrder (0)

ViewResolver.setContentType ("text/html;charset=UTF-8")

Return viewResolver

}

@ Bean

Public InternalResourceViewResolver viewResolver () {

InternalResourceViewResolver viewResolver = new InternalResourceViewResolver ()

/ / set the inspector

ViewResolver.setViewClass (HandleResourceViewExists.class)

ViewResolver.setPrefix ("/ WEB-INF/")

ViewResolver.setSuffix (".jsp")

ViewResolver.setOrder (0)

ViewResolver.setContentType ("text/html;charset=UTF-8")

Return viewResolver

}

This provides you with an idea, if you debug the code, find the problem, solve the problem.

On whether SpringBoot can support multiple view parsers at the same time jsp+html+ other template engines to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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: 271

*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

Servers

Wechat

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

12
Report