1. 程式人生 > >spring boot thymeleaf基本用法總結

spring boot thymeleaf基本用法總結

根據我看的文件,我建立了個簡單的spring boot +thymeleaf專案

pom.xml

<!--spring mvc必須的-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--thymeleaf 必須的-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Controller

package com.example.thymeleafdemo.controller;

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/th")
public class ThDemo1Controller {

    @RequestMapping("/mv")
    public ModelAndView mv(){
        ModelAndView mv = new ModelAndView();

        //單個詞
        mv.addObject("username","zhangsan");

        List<User> list = new ArrayList<User>();
        User user = new User();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-mm");
        for(int i = 0;i<100;i++){
            user.setId(i);
            user.setName("user," + i);
            try {
                Date d = df.parse("2" + i +"-11-11");
                user.setCreateTime(d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        list.add(user);
        mv.addObject("list",list);

        mv.addObject("last",user);

        mv.setViewName("a");
        return mv;
    }
}

實體類

package com.example.thymeleafdemo.entity;


import java.util.Date;

public class User {
    private Integer id;
    private String name;
    private Date createTime;

    //...getter,setter方法
}