1. 程式人生 > >spring boot與thymeleaf頁面傳參兩種方式

spring boot與thymeleaf頁面傳參兩種方式

1.利用ModelAndView物件向頁面傳參

@RequestMapping("/index/{p}.html")
public ModelAndView  index(@PathVariable int p,String keyword){
    ModelAndView view = new ModelAndView();
    view.setViewName("index");
    //因為用了spring boot快取,sb是用返回值做快取,所以service再次返回了pageQuery以快取查詢結果
    List<Topic> findTopicsByPage = topicService.findTopicsByPage(p,Const.TOPIC_PAGE_SIZE);
    view.addObject("topicPage", findTopicsByPage);
    view.addObject("pagename", "首頁綜合");
    return view;
}

2.利用model物件向頁面傳參

@RequestMapping("/index/{p}.html")

public String index(Model model){
//因為用了spring boot快取,sb是用返回值做快取,所以service再次返回了pageQuery以快取查詢結果
List<Topic> findTopicsByPage = topicService.findTopicsByPage(1,Const.TOPIC_PAGE_SIZE);
model.addAttribute("topicPage", findTopicsByPage);
model.addAttribute("pagename", "首頁綜合");
return "index";

}