<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Spring Boot Thymeleaf 配置 > 原文: [http://zetcode.com/articles/springbootthymeleafconf/](http://zetcode.com/articles/springbootthymeleafconf/) 在 Spring Boot Thymeleaf 配置教程中,我們將展示如何使用 Spring Boot Web 應用配置 Thymeleaf。 當 Spring Boot 在 Maven POM 文件中找到 Thymeleaf 依賴項時,它會自動配置 Thymeleaf 模板引擎。 本教程顯示了如何在 Java 配置中手動進行操作。 Spring 是流行的 Java 應用框架。 Spring Boot 致力于以最小的努力創建獨立的,基于生產級別的基于 Spring 的應用。 ## Thymeleaf Thymeleaf 是適用于 Web 和獨立環境的現代服務器端 Java 模板引擎。 它基于自然模板的概念:模板文件可以在瀏覽器中直接打開,并且仍然可以正確顯示為網頁。 ## Spring Boot Thymeleaf 示例 以下示例使用 Java 配置通過 Spring Boot 設置 Thymeleaf。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───config │ │ WebConfig.java │ └───resources │ └───mytemplates │ index.html └───test └───java ``` 這是項目結構。 Thymeleaf 模板文件位于自定義`src/main/resources/mytemplates`目錄中。 默認模板目錄為`src/main/resources/templates`。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>thymeleafconfigex</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 這是 Maven 構建文件。 `spring-boot-devtools`啟用熱插拔,禁用模板緩存并啟用實時重新加載。 `spring-boot-starter-thymeleaf`是使用 Thymeleaf 構建 Spring MVC 應用的入門工具。 `spring-boot-starter-web`是 Web 應用的啟動器。 `com/zetcode/config/WebConfig.java` ```java package com.zetcode.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Description; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.thymeleaf.spring5.SpringTemplateEngine; import org.thymeleaf.spring5.view.ThymeleafViewResolver; import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; @Configuration public class WebConfig implements WebMvcConfigurer { @Bean @Description("Thymeleaf template resolver serving HTML 5") public ClassLoaderTemplateResolver templateResolver() { var templateResolver = new ClassLoaderTemplateResolver(); templateResolver.setPrefix("mytemplates/"); templateResolver.setCacheable(false); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode("HTML5"); templateResolver.setCharacterEncoding("UTF-8"); return templateResolver; } @Bean @Description("Thymeleaf template engine with Spring integration") public SpringTemplateEngine templateEngine() { var templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); return templateEngine; } @Bean @Description("Thymeleaf view resolver") public ViewResolver viewResolver() { var viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine()); viewResolver.setCharacterEncoding("UTF-8"); return viewResolver; } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); } } ``` 在`WebConfig`中,我們配置 Thymeleaf 并設置主頁的視圖和控制器。 模板引擎是用 Java 代碼配置的。 ```java @Bean @Description("Thymeleaf template resolver serving HTML 5") public ClassLoaderTemplateResolver templateResolver() { ``` 這個 bean 定義了一個模板解析器。 模板解析器將模板解析為`TemplateResolution`對象,其中包含其他信息,例如模板模式,緩存,模板的前綴和后綴。 `ClassLoaderTemplateResolver`用于加載位于類路徑上的模板。 ```java templateResolver.setPrefix("mytemplates/"); ``` 我們將模板目錄設置為`mytemplates`。 使用`ClassLoaderTemplateResolver`時,前綴中沒有`classpath:`。 ```java templateResolver.setTemplateMode("HTML5"); ``` 模板引擎將提供 HTML5 內容。 ```java @Bean @Description("Thymeleaf template engine with Spring integration") public SpringTemplateEngine templateEngine() { var templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); return templateEngine; } ``` 創建具有 Spring 集成的 Thymeleaf 模板引擎。 ```java @Bean @Description("Thymeleaf view resolver") public ViewResolver viewResolver() { var viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine()); viewResolver.setCharacterEncoding("UTF-8"); return viewResolver; } ``` 在這里,我們配置一個創建`ThymeleafViewResolver`的 bean。 視圖解析器負責獲取特定操作和語言環境的 View 對象。 然后將視圖對象渲染為 HTML 文件。 ```java @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); } ``` 在這個簡單的應用中,我們沒有特定的控制器類。 我們用`addViewController()`方法定義一個自動控制器。 `resources/templates/index.html` ```java <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Home page</title> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> </head> <body> <p> <span th:text="'Today is: ' + ${#dates.format(#dates.createNow(), 'dd MMM yyyy HH:mm')}" th:remove="tag"></span> </p> </body> </html> ``` 這是 Thymeleaf 模板文件。 它顯示當前日期。 `com/zetcode/Application.java` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 這段代碼設置了 Spring Boot 應用。 `@SpringBootApplication`啟用自動配置和組件掃描。 ```java $ mvn spring-boot:run ``` 我們啟動該應用。 ```java $ curl localhost:8080 <!DOCTYPE html> <html> <head> <title>Home page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <p> Today is: 17 Jan 2019 23:46 </p> </body> ``` 在本教程中,我們使用 Thymeleaf 創建了 Spring Boot Web 應用。 您可能也對相關教程感興趣: [Spring Boot Thymeleaf 教程](/articles/springbootthymeleaf/), [Spring Boot 自動控制器](/articles/springbootautocontroller/), [Spring Boot FreeMarker 教程](/springboot/freemarker/), [Spring Boot Swing 集成教程](/articles/springbootswing/), [Spring Web 應用簡介](/articles/springwebfirst/),[獨立的 Spring 應用](/articles/standalonespring/), [FreeMarker 教程](/java/freemarker/)或 [Java 教程](/lang/java/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看