首页 > 代码库 > springMVC学习(4)-商品修改(RequestMapping解释、controller返回值)
springMVC学习(4)-商品修改(RequestMapping解释、controller返回值)
一、需求:
操作流程:
1、进入商品查询列表页面
2、点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询)
3、在商品修改页面,修改商品信息,修改后,点击提交
代码:
ItemsMapper.xml:--使用的是逆向工程生成的:
1 <mapper namespace="com.cy.mapper.ItemsMapper" > 2 <sql id="Base_Column_List" > 3 id, name, price, pic, createtime 4 </sql> 5 <sql id="Blob_Column_List" > 6 detail 7 </sql> 8 <resultMap id="BaseResultMap" type="com.cy.po.Items" > 9 <id column="id" property="id" jdbcType="INTEGER" /> 10 <result column="name" property="name" jdbcType="VARCHAR" /> 11 <result column="price" property="price" jdbcType="REAL" /> 12 <result column="pic" property="pic" jdbcType="VARCHAR" /> 13 <result column="createtime" property="createtime" jdbcType="TIMESTAMP" /> 14 </resultMap> 15 <resultMap id="ResultMapWithBLOBs" type="com.cy.po.Items" extends="BaseResultMap" > 16 <result column="detail" property="detail" jdbcType="LONGVARCHAR" /> 17 </resultMap> 18 <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" > 19 select 20 <include refid="Base_Column_List" /> 21 , 22 <include refid="Blob_Column_List" /> 23 from items 24 where id = #{id,jdbcType=INTEGER} 25 </select> 26 27 <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.cy.po.ItemsCustom" > 28 update items 29 set name = #{name,jdbcType=VARCHAR}, 30 price = #{price,jdbcType=REAL}, 31 pic = #{pic,jdbcType=VARCHAR}, 32 createtime = #{createtime,jdbcType=TIMESTAMP}, 33 detail = #{detail,jdbcType=LONGVARCHAR} 34 where id = #{id,jdbcType=INTEGER} 35 </update> 36 </mapper>
ItemsService.java:
1 public interface ItemsService { 2 3 public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; 4 5 //根据id查询商品信息 6 public ItemsCustom findItemsById(Integer id) throws Exception; 7 8 //修改商品信息 9 public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception; 10 }
ItemsServiceImpl.java:
1 package com.cy.service.impl; 2 3 import java.util.List; 4 5 import org.springframework.beans.BeanUtils; 6 import org.springframework.beans.factory.annotation.Autowired; 7 8 import com.cy.mapper.ItemsMapper; 9 import com.cy.mapper.ItemsMapperCustom; 10 import com.cy.po.Items; 11 import com.cy.po.ItemsCustom; 12 import com.cy.po.ItemsQueryVo; 13 import com.cy.service.ItemsService; 14 15 /** 16 * ItemsServiceImpl 17 * 18 */ 19 public class ItemsServiceImpl implements ItemsService { 20 21 @Autowired 22 private ItemsMapperCustom itemsMapperCustom; 23 24 @Autowired 25 private ItemsMapper itemsMapper; 26 27 @Override 28 public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { 29 //通过ItemsMapperCustom查询数据库 30 return itemsMapperCustom.findItemsList(itemsQueryVo); 31 } 32 33 @Override 34 public ItemsCustom findItemsById(Integer id) throws Exception { 35 Items items = itemsMapper.selectByPrimaryKey(id); 36 //中间对商品信息进行业务处理 37 //.... 38 //返回ItemsCustom 39 ItemsCustom itemsCoustom = new ItemsCustom(); 40 41 //将items的属性值拷贝到itemsCustom 42 BeanUtils.copyProperties(items, itemsCoustom); 43 return itemsCoustom; 44 } 45 46 @Override 47 public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception { 48 //添加业务校验,通常在service接口对关键参数进行校验 49 //校验 id是否为空,如果为空抛出异常 50 51 //更新商品信息使用updateByPrimaryKeyWithBLOBs根据id更新items表中所有字段,包括 大文本类型字段 52 //updateByPrimaryKeyWithBLOBs要求必须转入id 53 itemsCustom.setId(id); 54 itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom); 55 } 56 57 }
ItemsController.java:
1 @Controller 2 @RequestMapping("/items") 3 public class ItemsController { 4 5 @Autowired 6 private ItemsService itemsService; 7 8 @RequestMapping("/findItems") 9 public ModelAndView findItems() throws Exception { 10 11 List<ItemsCustom> itemsList = itemsService.findItemsList(null); 12 13 ModelAndView modelAndView = new ModelAndView(); 14 modelAndView.addObject("itemsList", itemsList); 15 modelAndView.setViewName("items/itemsList"); 16 return modelAndView; 17 } 18 19 //商品信息修改页面显示 20 @RequestMapping(value="http://www.mamicode.com/editItems",method={RequestMethod.POST,RequestMethod.GET}) 21 public String editItems(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception{ 22 ItemsCustom itemsCustom = itemsService.findItemsById(1); 23 24 //通过形参中的model将model数据传到页面 25 //相当于modelAndView.addObject方法 26 model.addAttribute("itemsCustom", itemsCustom); 27 28 return "items/editItems"; 29 } 30 31 //商品信息修改提交 32 @RequestMapping("/editItemsSubmit") 33 public String editItemsSubmit(HttpServletRequest request)throws Exception { 34 35 //重定向到商品查询列表 36 return "redirect:findItems.action"; 37 //页面转发 38 //return "forward:findItems.action"; 39 //return "success"; 40 } 41 }
/springMVC/WebRoot/WEB-INF/jsp/items/editItems.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>修改商品信息</title> 10 11 </head> 12 <body> 13 14 <form id="itemForm" action="${pageContext.request.contextPath }/items/editItemsSubmit.action" method="post" > 15 <input type="hidden" name="id" value="${itemsCustom.id }"/> 16 修改商品信息: 17 <table width="100%" border=1> 18 <tr> 19 <td>商品名称</td> 20 <td><input type="text" name="name" value="${itemsCustom.name }"/></td> 21 </tr> 22 <tr> 23 <td>商品价格</td> 24 <td><input type="text" name="price" value="${itemsCustom.price }"/></td> 25 </tr> 26 <tr> 27 <td>商品生产日期</td> 28 <td><input type="text" name="createtime" value="<fmt:formatDate value="http://www.mamicode.com/${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td> 29 </tr> 30 <%-- <tr> 31 <td>商品图片</td> 32 <td> 33 <c:if test="${item.pic !=null}"> 34 <img src="/pic/${item.pic}" width=100 height=100/> 35 <br/> 36 </c:if> 37 <input type="file" name="pictureFile"/> 38 </td> 39 </tr> --%> 40 <tr> 41 <td>商品简介</td> 42 <td> 43 <textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea> 44 </td> 45 </tr> 46 <tr> 47 <td colspan="2" align="center"><input type="submit" value="提交"/> 48 </td> 49 </tr> 50 </table> 51 52 </form> 53 </body> 54 55 </html>
items/itemsList.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>查询商品列表</title> 10 </head> 11 <body> 12 <form action="${pageContext.request.contextPath }/item/findItems.action" method="post"> 13 查询条件: 14 <table width="100%" border=1> 15 <tr> 16 <td><input type="submit" value="查询"/></td> 17 </tr> 18 </table> 19 商品列表: 20 <table width="100%" border=1> 21 <tr> 22 <td>商品名称</td> 23 <td>商品价格</td> 24 <td>生产日期</td> 25 <td>商品描述</td> 26 <td>操作</td> 27 </tr> 28 <c:forEach items="${itemsList }" var="item"> 29 <tr> 30 <td>${item.name }</td> 31 <td>${item.price }</td> 32 <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td> 33 <td>${item.detail }</td> 34 <td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a></td> 35 </tr> 36 </c:forEach> 37 </table> 38 </form> 39 </body> 40 </html>
二、@RequestMapping:
1)url映射
定义controller方法对应的url,进行处理器映射使用。
2)窄化请求映射
就像上面项目中的在ItemsContorller的类头上加上@RequestMapping("/items"),所有的方法的url路径就加上了/items;
3)限制http请求方法
@RequestMapping(value="http://www.mamicode.com/editItems",method={RequestMethod.POST,RequestMethod.GET})
三、Controller方法的返回值:
1)ModalAndView
2)返回String:
1.return String表示返回逻辑视图名。真正视图(jsp路径)=前缀+逻辑视图名+后缀
形参中定义Model model;model.addAttribute("itemsCustom", itemsCustom);
2.redirect重定向
redirect重定向特点:浏览器地址栏中的url会变化。修改提交的request数据无法传到重定向的地址。因为重定向后重新进行request(request无法共享)
3.forward页面转发
通过forward进行页面转发,浏览器地址栏url不变,request可以共享。
return "forward:findItems.action";
3)返回void:
在controller方法形参上可以定义request和response,使用request或response指定响应结果:
1、使用request转向页面,如下:
request.getRequestDispatcher("页面路径").forward(request, response);
2、也可以通过response页面重定向:
response.sendRedirect("url")
3、也可以通过response指定响应结果,例如响应json数据如下:
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
springMVC学习(4)-商品修改(RequestMapping解释、controller返回值)