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

  • 相关阅读:
    java中原子操作的实现分析
    AQS源码分析
    Java JNDI 学习
    门面模式、代理模式、适配器模式的区别
    tomcat源码阅读之StandardWrapper解析
    tomcat源码阅读之过滤器
    tomcat源码阅读之SingleThreadModel
    tomcat源码阅读之单点登录
    技术人员的八条宝贵经验,时刻提醒自己,共勉之
    tomcat源码阅读之安全机制
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12029162.html
Copyright © 2011-2022 走看看