Home > Java > javaTutorial > body text

Using Thymeleaf for Web template engine processing in Java API development

WBOY
Release: 2023-06-18 09:02:38
Original
1166 people have browsed it

In Web development, a template engine is a tool used to generate dynamic HTML. It combines data with HTML templates to generate the final HTML page. Thymeleaf is an emerging template engine. As a Java template engine, it supports HTML, XML, JSP, JavaScript and CSS. By separating data and templates, it makes generating HTML more flexible and easier to maintain.

This article mainly introduces how to use Thymeleaf to process the Web template engine in Java API development.

1. Introduction to Thymeleaf

Thymeleaf is a Java template engine that can be used in Web and non-Web environments. As a template engine for developers, Thymeleaf can achieve a series of benefits such as shortening the development cycle, reducing the amount of code, reducing external dependencies, and improving maintainability by integrating with frameworks such as Spring.

2. Use Thymeleaf for template engine processing

  1. Introduce Thymeleaf dependencies

Add dependencies in the pom.xml file:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.12.RELEASE</version>
</dependency>
Copy after login
  1. Configuring Thymeleaf view

To use Thymeleaf as the view parser, you need to configure it in Spring Boot:

@Configuration
public class ThymeleafConfig {
    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("classpath:/templates/");
        resolver.setSuffix(".html");
        resolver.setCharacterEncoding("UTF-8");
        resolver.setCacheable(false);
        return resolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }
}
Copy after login
  1. Write Thymeleaf template

Create an HTML file in the src/main/resources/templates directory:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>Thymeleaf Example</title>
</head>
<body>
  <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
Copy after login

Among them, xmlns:th="http://www.thymeleaf.org" means using Thymeleaf Tag, ${name} is the passed parameter.

  1. Processing templates

Handle the request in the Controller and render the template in Thymeleaf way:

@Controller
public class HelloController {
    @GetMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("name", "World");
        return "hello";
    }
}
Copy after login

Among them, "hello" The parameter indicates the template that needs to be rendered after processing the request.

  1. Run the program

Enter http://localhost:8080/hello in the browser, you can see the output: Hello, World!.

3. Conclusion

Thymeleaf’s simplicity and ease of use make it widely used in web development. This article mainly introduces the method of using Thymeleaf for Web template engine processing in Java API development. I hope it will be helpful.

The above is the detailed content of Using Thymeleaf for Web template engine processing in Java API development. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template