springboot(十一):springboot集成thymeleaf

1、创建maven项目并添加依赖

    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
    
    
    
        UTF-8
        UTF-8
        1.8
    
 
   
        
        
            org.springframework.boot
            spring-boot-starter-web
        
 
        
        
            org.projectlombok
            lombok
        

        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    

2、添加配置文件application.yml

# Thymeleaf
spring:
  thymeleaf:
    cache: true
    encoding: UTF-8
    mode: HTML5
    prefix: classpath:/templates/
    suffix: .html
    servlet:
      content-type: text/html

3、编写启动类

package com.wedu.thymeleaf;

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

@SpringBootApplication
public class ThymeleafApplication {

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

4、编写实体

package com.wedu.thymeleaf.pojo;

import lombok.Data;

import java.sql.Date;

/**
 * 用户信息实体
 */
@Data
public class UserInfo {

    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private Date regtime;
    private String siteaddress;
}

5、编写业务层代码

package com.wedu.thymeleaf.service;

import com.wedu.thymeleaf.pojo.UserInfo;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户信息业务层
 */
@Service
public class UserInfoService {

    public List findAll() {
        List userInfos = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(i);
            userInfo.setUsername("test" + i);
            userInfo.setPassword("98765" + i);
            userInfo.setAge(26 + i);
            userInfo.setRegtime(new Date(1234567890L));
            userInfo.setSiteaddress("https://www.csdn.net");
            userInfos.add(userInfo);
        }
        return userInfos;
    }
}

注:这里为了演示,直接手动造了一些数据。

6、编写控制层

package com.wedu.thymeleaf.web;

import com.wedu.thymeleaf.pojo.UserInfo;
import com.wedu.thymeleaf.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * 用户信息
 */
@Controller //注意:此处不能使用@RestController,@RestController只返回字符串,不能返回页面
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("findAll")
    public String findAll(Model model) {
        List userInfos = userInfoService.findAll();
        model.addAttribute("userInfos",userInfos);
        return "users";
    }
}

7、编写页面,并将model中数据渲染到页面




    
    首页
    


欢迎光临!
id 姓名 用户名 年龄 注册时间 网站地址
1 张三 zhangsan 20 1980-02-30 1

注意:页面使用thymeleaf,一定记得在页面的开始引用。

8、运行项目,打开页面访问地址:http://localhost:8080/findAll,显示结果如下:

springboot(十一):springboot集成thymeleaf_第1张图片

 

你可能感兴趣的:(springboot)