在controller中设置ModelAndView对象,根据view的名称和视图师徒解析器跳转到指定的页面。页面=视图解析器的前缀+viewname+视图解析器的后缀。
//商品查询 @RequestMapping("/queryItems") public ModelAndView queryItems(HttpServletRequest request) throws Exception{ System.out.println("重定向或者转发传来的商品名:" + request.getParameter("itemsName")); //调用service查找数据库,查询商品列表 List<ItemsCustom> itemsList = itemsService.findItemsList(); //返回ModelAndView ModelAndView mav = new ModelAndView(); //相当于request的setAttribute,在jsp页面能够可通过el表达式取出itemsList的值 mav.addObject("itemsList", itemsList); mav.setViewName("items/itemsList"); //指定视图, return mav; } 1234567891011121314151617 1234567891011121314151617ModelAndView和Model的用法差不多。ModelAndView使视图和返回数据可以集中在同一个变量,直接返回给前端。而使用Model,该类的对象只负责返回数据,具体的视图是通过返回String来完成。
@RequestMapping(value="/editItems", method={RequestMethod.POST, RequestMethod.GET}) public String editItems(Model model, String id) throws Exception { //调用service根据上平id查询商品信息 ItemsCustom itemsCustom = itemsService.findItemsById(Integer.parseInt(id)); model.addAttribute("itemsCustom", itemsCustom); return "items/editItems"; } 12345678 12345678商品修改提交后,重定向到商品查询列表。
redirect重定向特点:浏览器地址栏中的url会变化。修改提交的request数据无法传到重定向的地址。因为重定向后重新进行request(request无法共享)。
@RequestMapping(value="/edit", method={RequestMethod.POST, RequestMethod.GET}) public String edit(HttpServletRequest request, Model model) throws Exception { String itemsName = request.getParameter("itemsName"); System.out.println("将要修改的商品名称是:" + itemsName); //调用service,去修改对应的商品 itemsService.editItems(null); //重定向到查询列表中 return "redirect:queryItems"; } 1234567891011 1234567891011如上所诉,当修改商品信息之后,重定向到商品查询时,从request中取到的itemsName是null。
通过forward进行页面转发,浏览器地址栏url不变,request可以共享。
@RequestMapping(value="/edit", method={RequestMethod.POST, RequestMethod.GET}) public String edit(HttpServletRequest request, Model model) throws Exception { String itemsName = request.getParameter("itemsName"); System.out.println("将要修改的商品名称是:" + itemsName); //调用service,去修改对应的商品 itemsService.editItems(null); //转发到查询列表中 return "forward:queryItems"; } 12345678910 12345678910当修改商品信息之后,重定向到商品查询时,从request中取到的itemsName是刚才修改的商品的名称。
在controller方法形参上可以定义request和response,使用request或response指定响应结果:
使用request转向页面,如下: request.getRequestDispatcher(“页面路径”).forward(request, response);
也可以通过response页面重定向: response.sendRedirect(“url”)
也可以通过response指定响应结果,例如响应json数据如下: response.setCharacterEncoding(“utf-8”); response.setContentType(“application/json;charset=utf-8”); response.getWriter().write(“json串”);
@RequestMapping(value="/json", method={RequestMethod.POST, RequestMethod.GET}) public void json(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setCharacterEncoding("utf-8"); response.setContentType("application/json;charset=utf-8"); response.getWriter().write("hello json"); } 123456 123456修改商品信息的jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>订单修改</title> </head> <body> <center> <form action="edit" method="post"> <table width="20%" border="1px"> <tbody> <tr><td>id</td><td>${itemsCustom.id }</td></tr> <tr><td>商品名</td><td><input type="text" value="${itemsCustom.name }" name="itemsName" /></td></tr> <tr><td>价格</td><td><input type="text" value="${itemsCustom.price }" name="itemsPrice" /></td></tr> <tr><td>商品详情</td><td><input type="text" value="${itemsCustom.detail }" name="itemsDetail" /></td></tr> <tr><td>图片</td><td><input type="text" value="${itemsCustom.pic }" name="itemsPic" /></td></tr> <tr><td>订单创建时间</td><td>${itemsCustom.name }</td></tr> <tr><td colspan="2"><input type="submit" value="提交" /></td></tr> </tbody> </table> </form> </center> </body> </html> 1234567891011121314151617181920212223242526 1234567891011121314151617181920212223242526商品列表jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>商品列表</title> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script> <script type="text/javascript"> function testJson() { $.ajax({ type : "post", url : "${pageContext.request.contextPath}/items/json", contentType : "application/json;charset=utf-8", //数据格式是json串,传进一个person data : '{"userName" : "shizongger","gender" : "male"}', success:function(msg){ alert(msg); }, error:function(msg) { alert("出错了."); } }); } </script> </head> <body> <center> <table border="1px" width="80%"> <thead> <tr><th>序列</th><th>商品名称</th><th>商品详情</th><th>商品价格</th><th>订单创建时间</th><th>修改</th></tr> </thead> <tbody> <c:forEach items="${itemsList }" var="items" varStatus="status"> <tr> <td>${status.count }</td> <td>${items.name }</td> <td>${items.detail }</td> <td>${items.pic }</td> <td>${items.createtime }</td> <td><a href="editItems?id=${items.id }">修改</a></td> </tr> </c:forEach> </tbody> </table> </center> <button onclick="testJson()">测试json</button> </body> </html> 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849