In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Read the catalogue
I. Preface
Second, integrate Thymeleaf template engine
Third, use Thymeleaf template
Back to the top.
I. Preface
Thymeleaf emerged to replace JSP, and although JSP has been around for a long time and is ubiquitous in Java Web development, it also has some drawbacks:
1. The most obvious problem with JSP is that it looks like HTML or XML, but it is not. Most JSP templates are in the form of HTML, but they are mixed with various JSP tag libraries, which makes it very confusing.
2. JSP specification is closely coupled with Servlet specification. This means that it can only be used in Servlet-based Web applications. JSP templates cannot be used as general templates (such as formatting Email), nor can they be used in non-Servlet Web applications.
Compared with JSP, Thymeleaf solves these shortcomings well:
1. The Thymeleaf template is native and does not depend on the tag library. It can be edited and rendered where the original HTML is accepted.
2. Because it is not coupled to the Servlet specification, Thymeleaf templates can enter areas that JSP cannot enter. This means that Thymeleaf templates are different from JSP in that they can be edited or even rendered in the original way without going through any type of processor. Of course, we need Thymeleaf to process the template and render to get the final desired output. Even so, without any special handling, home.html can be loaded into a Web browser and looks similar to a full rendering.
Spring boot does not recommend using JSP to develop web.
Back to the top.
Second, integrate Thymeleaf template engine
SpringBoot's support for the Thymeleaf template engine is also simple:
1 、 pom.xml
Org.springframework.boot
Spring-boot-starter-thymeleaf
At this point, SpringBoot's support for Thymeleaf templates is complete, and we can use Thymeleaf templates in Web development, isn't it?
The previous article mentioned that the key to SpringBoot is "convention". Now that we have chosen such a simple configuration, we have to follow the SpringBoot Thymeleaf convention in our development. The most important thing is that the template files are placed in the templates directory, that is, the template parser prefix is / templates/ and the suffix is .html.
2. What if application.yml doesn't want the so-called conventional solution and wants to make some custom configurations? Look at the following: spring:
Thymeleaf:
Prefix: classpath:/templates/
Suffix: .html
Servlet:
Content-type: text/html
Enabled: true
Encoding: UTF-8
Mode: HTML5
Cache: false
3. WebConfig.java if the above configuration does not meet your requirements and you want to refine your control over Thymeleaf, including configuring view parsers, template parsers and template engines, please take a look at the following solution! / * * 1. ThymeleafViewResolver receives the logical view name and resolves it to view 2. SpringTemplateEngine enables the Thymeleaf engine in Spring to parse the template, and renders the results based on these templates. TemplateResolver will finally locate and find the template. , /
@ Configurationbr/ > * /
@ Configuration/** * configure Thymeleaf view parser-resolve logical view name to Thymeleaf template view * * @ param springTemplateEngine template engine * @ return * / @ Beanpublic ViewResolver viewResolver (SpringTemplateEngine springTemplateEngine) {ThymeleafViewResolver resolver = new ThymeleafViewResolver (); resolver.setTemplateEngine (springTemplateEngine); return resolver Template engine-process templates and render results * * @ param templateResolver template parser * @ return * / @ Beanpublic SpringTemplateEngine springTemplateEngine (ITemplateResolver templateResolver) {SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine (); springTemplateEngine.setTemplateResolver (templateResolver); return springTemplateEngine;} / * * template parser-load Thymeleaf template * * @ return * / @ Beanpublic ITemplateResolver templateResolver () {SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver () TemplateResolver.setPrefix ("classpath:/templates/"); templateResolver.setSuffix (".html"); templateResolver.setTemplateMode (TemplateMode.HTML); templateResolver.setCacheable (false); templateResolver.setTemplateMode ("HTML5"); return templateResolver;}
}
Third, use Thymeleaf template
With the above configuration in place, let's look at how to use the Thymeleaf template in SpringBoot:
1. Template file-/ templates/user/list.html
Insert title here
User list
-
-
-
2. Control layer-ModelAndViews here Model refers to: the control layer processes the request and returns the result to be rendered; Views refers to the logical view name of the template (the front and rear ends are separated). @ Controller
@ RequestMapping ("/ user")
Public class UserController {
@ RequestMapping ("/ list") public String listUser (Model model) {List userList = new ArrayList (); for (int I = 0; I
< 10; i++) { userList.add(new UserDto(UUID.randomUUID().toString().replace("-", ""), "张三" + i, 1, "中国北京")); } model.addAttribute("users", userList); return "user/list";} } 3、效果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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.