forward与redirect介绍 redirect 重定向,服务器收到请求后发送一个状态码给客户端,让客户端再重新请求,并且第一次请求中Request里的数据消失。所以redirect相当于客户端向服务器发出两次请求,第一次请求的数据不会转发给第二次请求,URL地址会变化两次。
forward 转发(前往),服务器内部的重定向,在Servlet中通过RequestDispatcher转发给另一个程序处理请求,请求的数据依然在。所以forward相当于客户端向服务器发送一次请求,服务器处理两次,请求数据不会消失且URL地址只变化一次。
Servlet与SpringMVC中的forward与redirect Servlet中的forward与redirect Servlet中的HttpServletResponse类中有sendRedirect(String location)方法直接重定向到URL为location的地址。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @WebServlet(name="deleteOneServlet",urlPatterns="/deleteOne.action") public class DeleteOneServlet extends HttpServlet { private static final long serialVersionUID = 1L ; private ContentService contentService = new ContentService (); @Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String id = req.getParameter("id" ); contentService.deleteOne(id); resp.sendRedirect("/list.action" ); } }
Servlet可以通过HttpServletRequest类的getRequestDispatcher(String path)获得RequestDispatcher对象,该通过该对象的forward(ServletRequest request, ServletResponse response)方法转发请求与相应给任何资源(如Servlet、HTML file、JSP file)。RequestDispatcher类的API如下:
1 2 3 4 5 6 7 8 9 10 package javax.servlet;public interface RequestDispatcher { public void forward (ServletRequest request, ServletResponse response) throws ServletException, IOException; public void include (ServletRequest request, ServletResponse response) throws ServletException, IOException; } API上的介绍: Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name. This interface is intended to wrap servlets, but a servlet container can create RequestDispatcher objects to wrap any type of resource.
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @WebServlet(name="query",urlPatterns="/query.action") public class QueryServlet extends HttpServlet { private static final long serialVersionUID = 1L ; private ContentService contentService = new ContentService (); @Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String command = req.getParameter("command" ); String description = req.getParameter("description" ); List <Message> messages = contentService.query(command, description); req.setAttribute("messages" , messages); req.setAttribute("command" , command); req.setAttribute("description" , description); req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp" ).forward(req, resp); } }
SpringMVC中的Servlet SpringMVC是基于Servlet的Web MVC框架,所以forward与redirect的处理结果一样,方式更为简单。SpringMVC中的InternalResourceViewResolver视图解析器会识别redirect与forward关键字,然后处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Controller @RequestMapping(value="/springMVC") public class UserController { @RequestMapping(value="/login") public String login () { return "login" ; } @RequestMapping(value="/upload", method=RequestMethod.POST) public String fileUpload (@RequestPart(value="file") MultipartFile multipartFile) throws Exception{ String path = "E:/java/fileupload/" + multipartFile.getOriginalFilename(); multipartFile.transferTo(new File (path)); return "redirect:/springMVC/index" ; return "foward:/springMVC/index" ; } }
SpringMVC重定向传递数据 从上面我们可以看出redirect不能传递数据,但我们可以使用其它方案传递数据。主要有:
通过URL模板以路径变量或查询参数形式传递数据
通过flash属性传递数据
通过URL模板进行重定向 方式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Controller @RequestMapping(value="/springMVC") public class UserController { @RequestMapping(value="/index/{name}",method=RequestMethod.GET) public String index (@PathVariable(value="name") String name, @RequestParam(value="id") int id) { System.out.println(name + id); return "login" ; } @RequestMapping(value="/data/{id}",method=RequestMethod.GET) public String data (@PathVariable(value="id") int id, Model model) { model.addAttribute("id" , id); model.addAttribute("name" , "Tom" ); return "redirect:/springMVC/index/{name}" ; } }
使用模板方式时,若使用了占位符则变为路径参数,否则变为请求变量。所以以上重定向URL路径变为”/springMVC/index/Tom?id=5”。 该方法简单有效,但传递数据值简单,若数据复杂则可使用下面的方式传递数据
使用flash属性 Spring提供了RedirectAttributes设置flash属性的方法重定向传递参数。 原理:在重定向执行之前,所有的flash属性会复制到session中。在重定向后,放在Session中的flash属性会被取出,放到Model中。注:RedirectAttributes类继承自Model类。方式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Controller @RequestMapping(value="/springMVC") public class UserController { @RequestMapping(value="/index",method=RequestMethod.GET) public String index (User user) { System.out.println(user); return "login" ; } @RequestMapping(value="/data/{id}",method=RequestMethod.GET) public String data (@PathVariable(value="id") int id, RedirectAttributes model) { User user = new User (id,"Tom" ); model.addFlashAttribute("user" , user); return "redirect:/springMVC/index" ; } }
References
http://blog.csdn.net/tenor/article/details/4077079