【Spring Boot教程】(十三):Spring Boot从后端传值给前端

前端直接使用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";
}

猜你喜欢

转载自blog.csdn.net/m0_46521785/article/details/114677426