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.运行 结果

  • 相关阅读:
    用U3D寻找看电视的感觉!!
    MipMap
    什么是 A 轮融资?有 B轮 C轮么?
    Java写的斗地主游戏源码
    sqlserver sp_spaceused用法
    SQL中的全局变量和局部变量(@@/@)
    SQL2008数据库导出到SQL2000全部步骤过程
    生成Insert语句的存储过程
    物理机连接虚拟机数据库
    配置sql server 2000以允许远程访问
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12029162.html
Copyright © 2011-2022 走看看