SpringBoot use thymeleaf Case

1 file written application.properties

CLASSPATH = spring.thymeleaf.prefix: / Templates / 
spring.thymeleaf.suffix = .html 
spring.thymeleaf.mode = the HTML5 
spring.thymeleaf.encoding = UTF . 8 
spring.thymeleaf.servlet.content -type = text / HTML 
#springboot official documents suggest that we close thymeleaf cache 
spring.thymeleaf.cache = false

 

 

2. Create entity classes

public class Student {
    private Integer stu_id;
    private String stu_name;

    public Integer getStu_id() {
        return stu_id;
    }

    public void setStu_id(Integer stu_id) {
        this.stu_id = stu_id;
    }

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public Student(Integer stu_id, String stu_name) {
        this.stu_id = stu_id;
        this.stu_name = stu_name;
    }
    public Student(){

    }
}

 

 

 

3 Create Controller layer

    @RequestMapping("/getStudents")
    public String getStudents(Model model){
        System.out.println("hello");
        List<Student> studentList=new ArrayList<>();
        Student student1=new Student(111,"张三");
        Student student2=new Student(222,"李四");
        Student student3=new Student(333,"王五");
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        model.addAttribute ("student",studentList);
        return "Hello";
    }

 

 

4. Write html page

<body>
    <table border="1">
        <tr>
            <td>学生编号</td>
            <td>学生姓名</td>
        </tr>
        <tr th:each="stu:${student}">
            <td th:text="${stu.stu_id}"></td>
            <td th:text="${stu.stu_name}"></td>
        </tr>
    </table>
</body>

 

 

5. Start program

@SpringBootApplication
public class StartSpringBoot {
    public static void main(String[] args) {
        SpringApplication.run(StartSpringBoot.class,args);
    }
}

 

 

6. Run results

 

 

Guess you like

Origin www.cnblogs.com/1314Justin/p/12029162.html