概述

在前后端不分离的时候,前端需要后端的值,我们今天康康后端如何向前端传值

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

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐