spring mvc 返回页面数据
handler
package com.stone.controller; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * user/list.do user/add.do */ @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/add1.do") public String add(HttpServletRequest request) { String userNum = request.getParameter("userNum"); String passWord = request.getParameter("passWord"); String x = "userNum:" + userNum + " passWord:" + passWord; System.out.println(x); request.setAttribute("user", x); return "user_add1"; } @RequestMapping("/add2.do") public ModelAndView add2() { ModelAndView mav = new ModelAndView("user_add1"); mav.addObject("userId", 1001); mav.addObject("userName", "张三"); return mav; } @RequestMapping("/add3.do") public String add3(Model model) { model.addAttribute("personId", 9527); model.addAttribute("personName", "唐伯虎");
//model.addAttribute("唐伯虎");==model.addAttribute("string","唐伯虎");
//model.addAttribute(new User()); == model.addAttribute("user",new User()); return "user_add1"; } @RequestMapping("/add4.do") public String add4(Map<String, Object> map) { // 这样是获取不到的; System.out.println("-----" + map.get("personId")); map.put("personId", 9528); map.put("personName", "唐伯虎1"); return "user_add1"; } }