package com.sf.springboot.controller;
import com.sf.springboot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@Controller
public class HelloWordController {
@GetMapping("/helloworld")
public String helloworld(){
System.out.println("helloworld");
return "ok";
}
@RequestMapping("/HelloThymeleaf")
public String HelloThymeleaf(Model model, HttpServletRequest request){
//显示普通文本,从域对象中获取
model.addAttribute("username","Tom");
//显示带有样式的普通文本
model.addAttribute("desc","你好,中国");
//显示对象,数据处理(thymeleaf提供了内置对象API可以操作数据,比如对前端显示时间的格式化)
User user = new User(1001,"user01",new Date());
model.addAttribute("user",user);
//内置域对象
request.setAttribute("password","123456");
//数据遍历 list
User user1 = new User(1002,"user02",new Date());
User user2 = new User(1003,"user03",new Date());
User user3 = new User(1004,"user04",new Date());
List users = new ArrayList<>();
users.add(user1);
users.add(user2);
users.add(user3);
model.addAttribute("users",users);
//数据遍历 map
Map map = new HashMap<>();
map.put("user01",user1);
map.put("user02",user2);
map.put("user03",user3);
model.addAttribute("map",map);
return "result";
}
}