public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public User(String name, Integer age) { super(); this.name = name; this.age = age; } public User() { super(); } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="user/add" method="post"> <!-- 必須是User類中對應的屬性名 --> 用戶名:<input type="text" name="name"> 年齡:<input type="text" name="age"> <button type="submit">提交</button> </form> </body> </html>
@Controller @RequestMapping("/user") public class MyController { /** * 轉發 默認格式 @RequestMapping(value = "/add") public ModelAndView add(User user) { System.out.println("進入了add......"); ModelAndView mv = new ModelAndView(); mv.addObject("name", user.getName()).addObject("age", user.getAge()) .setViewName("forward:/WEB-INF/jsp/success.jsp");// 設置返回的視圖 // .setViewName("/WEB-INF/jsp/success.jsp");// 默認就是轉發 return mv; }*/ /** * 重定向 : * 01.客戶端的行爲! 不能訪問/WEB-INF/下面的資源 * 02. 以訪問的路徑爲準,因此以前09項目中會有一個user/ * 03.還能攜帶上一次的數據 * 04.redirect:/success.jsp 加上/ 就是以項目的根目錄爲準 */ @RequestMapping(value = "/add") public ModelAndView add(User user) { System.out.println("進入了add......"); ModelAndView mv = new ModelAndView(); mv.addObject("name", user.getName()).addObject("age", user.getAge())// 攜帶數據 .setViewName("redirect:/success.jsp");// 設置返回的視圖 重定向 return mv; } }
<body> <!-- param.name 底層執行的代碼 就是request.getParameter("name") requestScope.nage 底層執行的代碼 就是request.getAttribute("name") 做用域查詢 --> <h1>webroot success頁面</h1> ${param.name}<br/> ${param.age} </body>
==================重定向到方法====================css
@Controller @RequestMapping("/user") public class MyController { /** * 跳轉到/list * Model:跳轉list方法時 攜帶的數據 */ @RequestMapping(value = "/add") public String add(User user, Model mv) { System.out.println("進入了add......"); // model中 增長數據 mv.addAttribute("name", user.getName()).addAttribute("age", user.getAge()); // 跳轉到list 不能加 / 這就是後臺路徑 return "redirect:list"; } @RequestMapping(value = "/list") public String list(User user) { System.out.println("進入了list......"); System.out.println(user.getName()); System.out.println(user.getAge()); return "/success.jsp";// 轉發到 webroot下的success.jsp } }