thymeleaf获取controller中的model值

  • 第一种方式:直接在html样式中获取
<span th:text="${name}"></span>
 
  • 第二种:js中直接获取
var msg = [[${msg}]];
 
  • 例子
@RequestMapping(value = "/index", method=RequestMethod.GET)
public ModelAndView loginSuccess( @RequestParam(value="username", required = true) String username, ModelAndView model) {
    model.setViewName("/index");
    model.getModel().put("username", username);  // 核心代码
    return model;
}
 
index.html
 
<span th:text="${username}" style="color:red; font-weight:bold;"></span>
 
发布了63 篇原创文章 · 获赞 1 · 访问量 2737

猜你喜欢

转载自blog.csdn.net/A_bad_horse/article/details/104861931