springMVC返回数据的四种方式

1、通过request域request.setAttribute()返回

@RequestMapping("/login")
public String index(HttpServletRequest request){
    request.setAttribute("name", "张三");
    request.setAttribute("role", "管理员");
    return "index";
}

2、通过ModelAndView对象返回

@RequestMapping(value="/login")
public ModelAndView WelcomeSeven(){
    //创建ModelAndView对象
    ModelAndView mav = new ModelAndView("index");
    mav.addObject("name", "张三");
    mav.addObject("role", "管理员");
    return mav;
}

3、通过model对象返回

@RequestMapping(value="/login")
public String WelcomeEight(Model model){
    model.addAttribute("name", "张三");
    model.addAttribute("role", "管理员");
    return "index";
}

4、通过Map对象进行返回

@RequestMapping(value="/login")
public String WelcomeNine(Map<String,String> map){
    map.put("name", "张三");
    map.put("role", "管理员");
    return "index";
}

猜你喜欢

转载自blog.csdn.net/itcats_cn/article/details/82119673