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>
==================重定向到方法====================
@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 } }