首页 > 编程语言 > 详细

SpringBoot2

时间:2019-12-14 11:05:54      阅读:83      评论:0      收藏:0      [点我收藏+]

一、thymeleaf模板

  1、在pom.xml中导入依赖

<!-- 添加thymeleaf模版的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

  2、配置application.yml文件

###配置thymeleaf
spring:
   thymeleaf:
      cache: false

  3、在resources目录下添加templates包,并在此包下创建index.html页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>thymeleaf模板页面</title>
</head>
<body>
    <ul th:each="stu:${stuList}">
        <li>
            <span th:text="${stu.stu_id}"></span>
            <span th:text="${stu.stu_name}"></span>
        </li>
    </ul>
</body>
</html>

4、创建启动类

package com.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

5、创建实体类student

package com.boot.entity;

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;
    }
}

  

6、创建Controller类

 
package com.boot.controller;

import com.boot.entity.student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/thymeleafController")
public class thymeleafController {

    @RequestMapping("/thymeleaf")
    public String thymeleaf(Model model){
        List<student> list=new ArrayList<>();
        student stu1=new student(1,"张三");
        student stu2=new student(2,"李四");
        student stu3=new student(3,"王五");
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        model.addAttribute("stuList",list);
        return "index";
    }
}

SpringBoot2

原文:https://www.cnblogs.com/cw172/p/12038060.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!