Spring Boot从后端传值给前端
概述在前后端不分离的时候,前端需要后端的值,我们今天康康后端如何向前端传值前端直接使用Thymeleaf语法即可使用ModelAndView+MapModelAndView是用来返回页面的,防止添加了@RestController注解@GetMapping("/seller/logout")public ModelAndView logout(Map<String,Object> ma
·
概述
在前后端不分离的时候,前端需要后端的值,我们今天康康后端如何向前端传值
前端直接使用Thymeleaf语法即可
使用ModelAndView+Map
ModelAndView是用来返回页面的,防止添加了@RestController
注解
@GetMapping("/seller/logout")
public ModelAndView logout(Map<String,Object> map){
map.put("msg","登出!");
map.put("url","/sell");
return new ModelAndView("common/success",map);
}
使用HttpServletRequest
注意不要加@RestController
注解
@GetMapping("/index")
public Object index(HttpServletRequest request) {
//先获取principal,这个是通过MyRealm的认证方法rutuen的,进行了注入
Object principal = SecurityUtils.getSubject().getPrincipal();
AccountProfile user = (AccountProfile) principal;
//添加session
request.setAttribute("username",user.getUsername());
return "index";
}
使用Model
@RequestMapping(value = "/")
public String index(Model model){
String students ="刘洋";
model.addAttribute("s",students)
return "index";
}
更多推荐
已为社区贡献8条内容
所有评论(0)