【转】SpringMVC框架实现后端向前端传数据

首先还是页面userAdd.jsp。它既是发出请求的页面,也是接收返回结果的页面:

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <h1>添加用户信息4</h1>
    <form action="user/add4.do" method="post">
        <input type="submit" value="提交">
    </form>
    ${personId }
  </body>
</html>
1、通过request对象:
@RequestMapping("/add")
public String add(HttpServletRequest request){
    request.setAttribute("personId",12);
    return "userAdd";
}
2、通过ModelAndView对象:
@RequestMapping("/add")
public ModelAndView add(){
    ModelAndView mav = new ModelAndView("userAdd");
    mav.addObject("personId", 12);
    return mav;
}
3、通过Model对象:
@RequestMapping("/add")
public String add(Model model){
    model.addAttribute("personId", 12);
    return "userAdd";
}
4、通过Map对象:
@RequestMapping("/add")
public String add(Map<String,Object> map){
    map.put("personId", 12);
    return "userAdd";
}

原文链接:https://blog.csdn.net/qq_31634461/article/details/81119228

 

posted @ 2019-11-01 16:22  江云风  阅读(710)  评论(0编辑  收藏  举报
// 点击-心形效果