SpringMVC之返回頁面資料
阿新 • • 發佈:2018-11-07
1. HttpServletRequest request 直接使用request.setAttribute(Key, Value);
jsp頁面:
<form action="user/add1.do" method="post"> 賬號:<input type="text" name="userNumber" /><br/> 密碼:<input type="text" name="passWord" /><br/> <input type="submit" value="提交"/> </form> ${user }
Controller:
@RequestMapping(value="/add1.do") public String add(HttpServletRequest request){ String userNumber = request.getParameter("userNumber"); String passWord = request.getParameter("passWord"); String str = "userNumber:"+userNumber+" passWord:"+passWord; request.setAttribute("user", str); return "user_add1"; }
2. 使用ModelAndView
jsp頁面:
<form action="user/add2.do" method="post">
<input type="submit" value="提交"/>
</form>
${userId }---${userName }
@RequestMapping(value="/add2.do") public ModelAndView add2(){ ModelAndView mav = new ModelAndView("user_add2"); mav.addObject("userId", 1001); mav.addObject("userName", "張三"); return mav; }
3. 使用Model model
jsp頁面:
<form action="user/add3.do" method="post">
<input type="submit" value="提交"/>
</form>
${personId }----${personName }
@RequestMapping(value="/add3.do")
public String add3(Model model){
model.addAttribute("personId", 9527);
model.addAttribute("personName", "唐伯虎");
return "user_add3";
}
4. 使用Map
jsp頁面:
<h1>使用者資訊新增4</h1>
<form action="user/add4.do" method="post">
編號:<input type="text" name="personId" /><br/>
編號:<input type="text" name="userName" /><br/>
<input type="submit" value="提交"/>
</form>
${personId }----${personName }<br/>
${userName }
@RequestMapping(value="/add4.do")
public String add4(Integer personId,String userName,Map map){
System.out.println(personId);
System.out.println(userName);
map.put("personId", 9527);
map.put("personName", "唐伯虎");
return "user_add4";
}