博客信息

springboot之thymeleaf模板

发布时间:『 2019-02-14 22:38』  博客类别:SpringBoot  阅读(958)
关于Thymeleaf的优点,我只说一条:它就是html页面。下面直接上代码

相关pom依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
spring.thymeleaf.cache=false
正式环境还是要将缓存开启的


对应的后台代码
package com.javaxl.springboot01.entity;

public class User {
    private Integer uid;
    private String userName;
    private String password;

    public User(Integer uid, String userName, String password) {
        this.uid = uid;
        this.userName = userName;
        this.password = password;
    }

    public User() {
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
package com.javaxl.springboot01.controller;

import com.javaxl.springboot01.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

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

@Controller
public class IndexController {

    @RequestMapping("/user/list")
    public ModelAndView index(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/user/list");
        List list = new ArrayList();
        list.add(new User(1,"张三","123"));
        list.add(new User(2,"李四","456"));
        mav.addObject("userList",list);
        mav.addObject("msg","用户列表");
        return mav;
    }
}

前台HTML页面
<html xmlns:th="http://www.thymeleaf.org">

小李飞刀_SpringBoot小李飞刀_SpringBoot

小李飞刀_SpringBoot



浏览器访问结果

小李飞刀_SpringBoot



关键字:     SpringBoot  

备案号:湘ICP备19000029号

Copyright © 2018-2019 javaxl晓码阁 版权所有