In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of the sample analysis of the Thymeleaf template engine in springboot. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Template engine
Springboot is currently packaged in the form of a jar package. In fact, we packed it into a war package and put it in a tomcat server. We can use JSP.
But the jar package makes it impossible to use JSP. Another way is the Thymeleaf template engine recommended by springboot (JSP is also a template engine, and what else framework is also a template engine)
What is a template engine?
The template engine is a solution that we need to dynamically assign values to the front end.
The function of the template engine is that we write a page template, for example, some values are dynamic, we write some expressions. And where do these values come from, that is, we encapsulate some data in the background. Then give this template and this data to our template engine, according to our data, the template engine will help you parse the expression, fill it to our specified location, and then finally generate a content that we want to write out to us. This is our template engine, whether it is jsp or other template engines. It's just, that is to say, between different template engines, their syntax may be a little different. I will not introduce the rest. I will mainly introduce the Thymeleaf template engine recommended to us by SpringBoot. This template engine is a template engine for a high-level language, and its syntax is simpler. Moreover, the function is more powerful. )
The principle of template engine
Introduction of Thymeleaf
To introduce is to import dependencies. The following URL is the relevant address.
Thymeleaf official website: https://www.thymeleaf.org/
Thymeleaf's home page in Github: https://github.com/thymeleaf/thymeleaf
Spring official documentation: find our corresponding version
Https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter
Find the corresponding pom dependency: you can click the source code to take a look at the original package!
Org.springframework.boot spring-boot-starter-thymeleafThymeleaf analysis
The introduction of Thymeleaf depends on the file ThymeleafProperties, which contains the following contents:
/ / IntelliJ API Decompiler stub source generated from a class file / / Implementation of methods is not availablepackage org.springframework.boot.autoconfigure.thymeleaf;@org.springframework.boot.context.properties.ConfigurationProperties (prefix = "spring.thymeleaf") public class ThymeleafProperties {private static final java.nio.charset.Charset DEFAULT_ENCODING; public static final java.lang.String DEFAULT_PREFIX = "classpath:/templates/"; public static final java.lang.String DEFAULT_SUFFIX = ".html"; private boolean checkTemplate; private boolean checkTemplateLocation Private java.lang.String prefix; private java.lang.String suffix; private java.lang.String mode; private java.nio.charset.Charset encoding; private boolean cache; private java.lang.Integer templateResolverOrder; private java.lang.String [] viewNames; private java.lang.String [] excludedViewNames; private boolean enableSpringElCompiler; private boolean renderHiddenMarkersBeforeCheckboxes; private boolean enabled; private final org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties.Servlet servlet Private final org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties.Reactive reactive Public ThymeleafProperties () {/ * compiled code * /} public boolean isEnabled () {/ * compiled code * /} public void setEnabled (boolean enabled) {/ * compiled code * /} public boolean isCheckTemplate () {/ * compiled code * /} public void setCheckTemplate (boolean checkTemplate) {/ * compiled code * /} public boolean isCheckTemplateLocation () {/ * compiled code * /} public void setCheckTemplateLocation (boolean checkTemplateLocation) {/ * compiled code * /} public java.lang.String getPrefix () {/ * compiled code * /} public void setPrefix (java.lang.String prefix) {/ * compiled code * /} public java.lang.String getSuffix () {/ * compiled code * /} public void setSuffix (java.lang.String suffix) {/ * compiled code * /} public java.lang.String getMode () {/ * compiled Code * /} public void setMode (java.lang.String mode) {/ * compiled code * /} public java.nio.charset.Charset getEncoding () {/ * compiled code * /} public void setEncoding (java.nio.charset.Charset encoding) {/ * compiled code * /} public boolean isCache () {/ * compiled code * /} public void setCache (boolean cache) {/ * compiled code * /} public Java.lang.Integer getTemplateResolverOrder () {/ * compiled code * /} public void setTemplateResolverOrder (java.lang.Integer templateResolverOrder) {/ * compiled code * /} public java.lang.String [] getExcludedViewNames () {/ * compiled code * /} public void setExcludedViewNames (java.lang.String [] excludedViewNames) {/ * compiled code * /} public java.lang.String [] getViewNames () {/ * compiled code * /} public Void setViewNames (java.lang.String [] viewNames) {/ * compiled code * /} public boolean isEnableSpringElCompiler () {/ * compiled code * /} public void setEnableSpringElCompiler (boolean enableSpringElCompiler) {/ * compiled code * /} public boolean isRenderHiddenMarkersBeforeCheckboxes () {/ * compiled code * /} public void setRenderHiddenMarkersBeforeCheckboxes (boolean renderHiddenMarkersBeforeCheckboxes) {/ * compiled code * /} public org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties.Reactive getReactive () { / * compiled code * /} public org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties.Servlet getServlet () {/ * compiled code * /} public static class Reactive {private org.springframework.util.unit.DataSize maxChunkSize Private java.util.List mediaTypes; private java.lang.String [] fullModeViewNames; private java.lang.String [] chunkedModeViewNames Public Reactive () {/ * compiled code * /} public java.util.List getMediaTypes () {/ * compiled code * /} public void setMediaTypes (java.util.List mediaTypes) {/ * compiled code * /} public org.springframework.util.unit.DataSize getMaxChunkSize () {/ * compiled code * /} public void setMaxChunkSize (org.springframework.util.unit.DataSize maxChunkSize) {/ * compiled code * /} public java.lang.String [] getFullModeViewNames () {/ * compiled code * /} public void setFullModeViewNames (java.lang.String [] fullModeViewNames) {/ * compiled code * /} public java.lang.String [] getChunkedModeViewNames () {/ * compiled code * /} public void setChunkedModeViewNames (java.lang.String [] chunkedModeViewNames) {/ * compiled code * /}} Public static class Servlet {private org.springframework.util.MimeType contentType Private boolean producePartialOutputWhileProcessing; public Servlet () {/ * compiled code * /} public org.springframework.util.MimeType getContentType () {/ * compiled code * /} public void setContentType (org.springframework.util.MimeType contentType) {/ * compiled code * /} public boolean isProducePartialOutputWhileProcessing () {/ * compiled code * /} public void setProducePartialOutputWhileProcessing (boolean producePartialOutputWhileProcessing) {/ * compiled code * /}}
We can see the default prefixes and suffixes in it.
We just need to put our html page under templates under the classpath, and thymeleaf can help us render automatically.
Start the access result
Thymeleaf grammar learning
To learn grammar, it is most accurate to refer to the documents on the official website. Let's find the corresponding version and have a look.
Thymeleaf official website: https://www.thymeleaf.org/, take a brief look at the official website! Let's download the official documentation of Thymeleaf!
Do the simplest operation of passing a value to the page
(1) write a page and a msg to pass the value
ZP Test Page
Code
Note that the string of return should be the same as the name of the html file under the templates above.
Package com.example.zpspringboot3.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;/** * @ Author: zhangpeng * @ Date: 16:02 on 2022-2-21 * / @ Controllerpublic class TestController {/ @ RequestMapping ("/ T1") / / public String test1 () {/ classpath:/templates/test.html// return "text" / / @ RequestMapping ("/ T1") public String test1 (Model model) {/ / store data model.addAttribute ("msg", "Hello,Thymeleaf"); / / classpath:/templates/test.html return "text2";}}
test
Learn some grammars from the thymeleaf official website documentation:
1. We can use any th:attr to replace the value of the native attribute in Html!
2. What expressions can we write?
Simple expressions: (expression syntax) Variable Expressions: ${...}: get the variable value; OGNL; 1), get the properties of the object, call method 2), use the built-in base object: # 18 # ctx: the context object. # vars: the context variables. # locale: the context locale. # request: (only in Web Contexts) the HttpServletRequest object. # response: (only in Web Contexts) the HttpServletResponse object. # session: (only in Web Contexts) the HttpSession object. # servletContext: (only in Web Contexts) the ServletContext object. 3), some built-in tool objects: # execInfo: information about the template being processed. # uris: methods for escaping parts of URLs/URIs # conversions: methods for executing the configured conversion service (if any). # dates: methods for java.util.Date objects: formatting, component extraction, etc. # calendars: analogous to # dates But for java.util.Calendar objects. # numbers: methods for formatting numeric objects. # strings: methods for String objects: contains, startsWith, prepending/appending Etc. # objects: methods for objects in general. # bools: methods for boolean evaluation. # arrays: methods for arrays. # lists: methods for lists. # sets: methods for sets. # maps: methods for maps. # aggregates: methods for creating aggregates on arrays or collections.==== Selection Variable Expressions: * {...}: selection expression: the same function as ${} Message Expressions: # {...}: get internationalized content Link URL Expressions: @ {...}: define URL; Fragment Expressions: ~ {...}: fragment reference expression Literals (literally) Text literals: 'one text',' Another OneQuest', … Number literals: 0,34,3.0,12.3, … Boolean literals: true, false Null literal: null Literal tokens: one, sometext, main, … Text operations: (text operation) String concatenation: + Literal substitutions: | The name is ${name} | Arithmetic operations: (mathematical operation) Binary operators: +, -, *, /,% Minus sign (unary operator):-Boolean operations: (Boolean operation) Binary operators: and, or Boolean negation (unary operator):!, notComparisons and equality: (comparison operation) Comparators: >
< , >=
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.