zoukankan      html  css  js  c++  java
  • SpringBoot使用thymeleaf案例

    1 编写application.properties文件

    spring.thymeleaf.prefix=classpath:/templates/
    spring.thymeleaf.suffix=.html
    spring.thymeleaf.mode=HTML5
    spring.thymeleaf.encoding=UTF-8
    spring.thymeleaf.servlet.content-type=text/html
    #springboot 官方文档建议我们关闭thymeleaf的缓存
    spring.thymeleaf.cache=false

    2.创建实体类

    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  创建Controller层

        @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.编写html页面

    <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.启动程序

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

    6.运行 结果

  • 相关阅读:
    Hibernate: Encountered a duplicated sql alias [] during auto-discovery of a native-sq
    “Uncaught TypeError: string is not a function”
    Jquery Ajax 返回数据类型变成document
    浏览器URL编码
    SQL Server 多条查询结果组合
    (转)No row with the given identifier exists问题的解决
    观nginx与lvs负载均衡的较量
    Nginx启动、关闭、重新加载脚本
    数据挖掘-分词入门
    HBase 专题技术收录
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12029162.html
Copyright © 2011-2022 走看看