程序员社区

【SpringMvc学习及总结10】SpringMvc如何将数据响应给页面

文章目录

        • 1.在方法上传入Map/Model/ModelMap
        • 2.方法返回ModelAndView对象

1.在方法上传入Map/Model/ModelMap

在方法上传入Map/Model/ModelMap后,这些参数里面保存的数据都会放在request域中,可以在页面获取。

<a href="hello01">hello01</a><br>
<a href="hello02">hello02</a><br>
<a href="hello03">hello03</a><br>
@Controller
public class HelloController {
    @RequestMapping("/hello01")
    public String test1(Map<String,Object> map){
        map.put("username","张三");
        return "success";
    }
    @RequestMapping("/hello02")
    public String test2(Model model){
        model.addAttribute("username","李四");
        return "success";
    }

    @RequestMapping("/hello03")
    public String test3(ModelMap modelMap){
        modelMap.addAttribute("username","赵丽");
        return "success";
    }
}

请求转发页面:

${username}

在这里插入图片描述

2.方法返回ModelAndView对象

@Controller
public class HelloController {
    @RequestMapping("/hello04")
    public ModelAndView test4(){
        ModelAndView modelAndView= new ModelAndView("success");
        modelAndView.addObject("username","郑爽");
        return modelAndView;
    }
}
${username}

既包含视图信息(页面地址),也包含模型数据(给页面带的数据),数据保存在request域中

赞(0) 打赏
未经允许不得转载:IDEA激活码 » 【SpringMvc学习及总结10】SpringMvc如何将数据响应给页面

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区