zoukankan      html  css  js  c++  java
  • Spring Boot Web开发中Thymeleaf模板引擎的使用

    这里使用的是idea

    1.新建Spring Boot项目

      File-->New-->Project...,然后选择左边的Spring Initializr-->Next,可根据自己的需求修改,也可默认,-->Next,选择依赖项。-->Template Engines中的Thymeleaf-->Next-->finish。

    2.建立实体类

    package com.example.thymeleaf.entity;
    
    public class Person {
        private String name;
        private Integer age;
    
        public Person(){
            super();
        }
    
        public Person(String name,Integer age){
            super();
            this.age=age;
            this.name=name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

    2.在ThymeleafApplication.java文件中,修改如下

    package com.example.thymeleaf;
    
    import com.example.thymeleaf.entity.Person;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @SpringBootApplication
    public class ThymeleafApplication {
    
        @RequestMapping("/")
        public String index(Model model){
            Person single = new Person("aa",11);
            model.addAttribute("singlePerson",single);
            return "index";
        }
    
        public static void main(String[] args) {
            SpringApplication.run(ThymeleafApplication.class, args);
        }
    }

    3.在resource-->templates下建立index.html文件,内容如下。

    <!DOCTYPE html>
    <html  xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Hello World!</title>
    </head>
    <body>
    <p th:text="${singlePerson.name}"></p>
    </body>
    </html>

    Thymeleaf是一个Java类库,我的理解是类似于JSP文件也类似与Anjularjs。如果想了解详解内容请到官网查看:http://www.thymeleaf.org/

    4.浏览器中输入localhost:8080显示内容如下

    aa

  • 相关阅读:
    Running APP 使用说明
    Android 控件八 WebView 控件
    Android 控件七 ImageView 控件
    Android 控件六 CheckBox 控件
    Android 控件五 RadioButton 控件
    Android 控件四 EditText 控件
    Android 控件三 TextView 控件实现 Button
    Android 控件二 Button
    Android 基础控件演示实例
    Android 控件一 TextView
  • 原文地址:https://www.cnblogs.com/liter7/p/7265451.html
Copyright © 2011-2022 走看看