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 create a Spring Boot project with Thymeleaf

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "how to use Thymeleaf to create a Spring Boot project", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Thymeleaf to create a Spring Boot project" article.

Thymeleaf is a presentation layer template engine, which is generally used in the Web environment.

Thymeleaf supports a variety of template types: HTML, XML, JavaScript, CSS and plain text, and six template processing modes are provided by default: HTML, XML, TEXT, JAVASCRIPT, CSS and RAW. The last RAW indicates that the template will not be processed.

Development environment: IntelliJ IDEA 2019.2.2

Spring Boot version: 2.1.8

Create a new Spring Boot project named demo.

Pom.xml joins Thymeleaf dependency:

Org.thymeleaf thymeleaf 3.0.11.RELEASE

First, deal with HTML

Create a new class HtmlTest.java

Package com.example.demo;import org.thymeleaf.TemplateEngine;import org.thymeleaf.context.Context;public class HtmlTest {public static void main (String [] args) {TemplateEngine templateEngine = new TemplateEngine (); Context ctx = new Context (); String html = ""; String result = templateEngine.process (html, ctx); System.out.println (result);}}

Right-click Run 'XmlTest.main ()' and output from the console:

Based on the output, the TemplateEngine class converts the HTML code with Thymeleaf logic into pure HTML output, which is what the template engine does.

Second, replace the template parser

By default, the template processing mode is HTML, and different parsers can be set.

Create a new class XmlTest.java

Package com.example.demo;import org.thymeleaf.TemplateEngine;import org.thymeleaf.context.Context;import org.thymeleaf.templatemode.TemplateMode;import org.thymeleaf.templateresolver.StringTemplateResolver;public class XmlTest {public static void main (String [] args) {TemplateEngine templateEngine = new TemplateEngine (); / / create a template parser StringTemplateResolver resolver = new StringTemplateResolver (); / / set the template mode to XML resolver.setTemplateMode (TemplateMode.XML) / / set the parser to the engine instance templateEngine.setTemplateResolver (resolver); Context ctx = new Context (); String xml = ""; String result = templateEngine.process (xml, ctx); System.out.println (result);}}

Right-click Run 'HtmlTest.main ()' and output from the console:

Third, deal with resource files

You can set up a resource parser for the template engine to find files for processing.

1. Create a new file index.html under src/main/resources and keep only the following line of code

2. Create a new class ClassLoaderTest.java

Package com.example.demo;import org.thymeleaf.TemplateEngine;import org.thymeleaf.context.Context;import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;public class ClassLoaderTest {public static void main (String [] args) {TemplateEngine templateEngine = new TemplateEngine (); / / create the parser ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver (); / / set the parser to the engine instance templateEngine.setTemplateResolver (resolver) / / deal with index.html String result = templateEngine.process ("index.html", new Context ()); System.out.println (result);}} under classpath

Right-click Run 'ClassLoaderTest.main ()' and output from the console:

Fourth, variable processing

You can set the value of a variable through an Context instance.

1. Create a new file var.html under src/main/resources

2. Create a new class VarTest.java

Package com.example.demo;import org.thymeleaf.TemplateEngine;import org.thymeleaf.context.Context;import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;public class VarTest {public static void main (String [] args) {TemplateEngine templateEngine = new TemplateEngine (); ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver (); templateEngine.setTemplateResolver (resolver); Context ctx = new Context (); ctx.setVariable ("userName", "lc") String result = templateEngine.process ("var.html", ctx); System.out.println (result);}}

Right-click Run 'VarTest.main ()' and output from the console:

Fifth, traverse the set

1. Create a new file iteration.html under src/main/resources

Name

2. Create a new class IterationTest.java

Package com.example.demo;import org.thymeleaf.TemplateEngine;import org.thymeleaf.context.Context;import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;import java.util.ArrayList;import java.util.List;public class IterationTest {public static void main (String [] args) {TemplateEngine templateEngine = new TemplateEngine (); ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver (); templateEngine.setTemplateResolver (resolver); List datas = new ArrayList (); datas.add (Zhang San) Datas.add ("Li Si"); Context ctx = new Context (); ctx.setVariable ("datas", datas); ctx.setVariable ("username", "lc"); String result = templateEngine.process ("iteration.html", ctx); System.out.println (result);}}

Right-click Run 'IterationTest.main ()' and output from the console:

Zhang San and Li Si

Setting prefixes and suffixes

1. Create a new file index.html under src/main/resources/templates

2. Create a new class PrefixSuffixTest.java

Package com.example.demo;import org.thymeleaf.TemplateEngine;import org.thymeleaf.context.Context;import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;public class PrefixSuffixTest {public static void main (String [] args) {TemplateEngine templateEngine = new TemplateEngine (); ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver (); resolver.setPrefix ("/ templates/"); resolver.setSuffix (".html"); templateEngine.setTemplateResolver (resolver) String result = templateEngine.process ("index", new Context ()); System.out.println (result);}}

Right-click Run 'PrefixSuffixTest.main ()' and output from the console:

VII. Integrating Thymeleaf in Spring Boot

1. Pom.xml joins Thymeleaf dependency:

Org.springframework.boot spring-boot-starter-thymeleaf

2 、 application.yml

Note: the values configured below are all default values, but they are not actually configured.

Spring: thymeleaf: mode: HTML prefix: classpath:/templates/ suffix: .html

3. Create a new file test.html under src/main/resources/templates

Title

4. Create a new controller class DemoController.java

Package com.example.demo;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class DemoController {@ RequestMapping ("/ test") public String test (Model model) {model.addAttribute ("userName", "lc"); return "test";}}

After running, the browser accesses http://localhost:8080/test

You can see the output.

The above is about the content of this article on "how to create a Spring Boot project with Thymeleaf". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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