springMVC学习(4)-商品修改(RequestMapping解释、controller返回值)
一、需求:
操作流程:
1、进入商品查询列表页面
2、点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询)
3、在商品修改页面,修改商品信息,修改后,点击提交
代码:
ItemsMapper.xml:--使用的是逆向工程生成的:
<mapper namespace="com.cy.mapper.ItemsMapper" >
<sql id="Base_Column_List" >
id, name, price, pic, createtime
</sql>
<sql id="Blob_Column_List" >
detail
</sql>
<resultMap id="BaseResultMap" type="com.cy.po.Items" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="price" property="price" jdbcType="REAL" />
<result column="pic" property="pic" jdbcType="VARCHAR" />
<result column="createtime" property="createtime" jdbcType="TIMESTAMP" />
</resultMap>
<resultMap id="ResultMapWithBLOBs" type="com.cy.po.Items" extends="BaseResultMap" >
<result column="detail" property="detail" jdbcType="LONGVARCHAR" />
</resultMap>
<select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from items
where id = #{id,jdbcType=INTEGER}
</select> <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.cy.po.ItemsCustom" >
update items
set name = #{name,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
pic = #{pic,jdbcType=VARCHAR},
createtime = #{createtime,jdbcType=TIMESTAMP},
detail = #{detail,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
ItemsService.java:
public interface ItemsService { public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; //根据id查询商品信息
public ItemsCustom findItemsById(Integer id) throws Exception; //修改商品信息
public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception;
}
ItemsServiceImpl.java:
package com.cy.service.impl; import java.util.List; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import com.cy.mapper.ItemsMapper;
import com.cy.mapper.ItemsMapperCustom;
import com.cy.po.Items;
import com.cy.po.ItemsCustom;
import com.cy.po.ItemsQueryVo;
import com.cy.service.ItemsService; /**
* ItemsServiceImpl
*
*/
public class ItemsServiceImpl implements ItemsService { @Autowired
private ItemsMapperCustom itemsMapperCustom; @Autowired
private ItemsMapper itemsMapper; @Override
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
//通过ItemsMapperCustom查询数据库
return itemsMapperCustom.findItemsList(itemsQueryVo);
} @Override
public ItemsCustom findItemsById(Integer id) throws Exception {
Items items = itemsMapper.selectByPrimaryKey(id);
//中间对商品信息进行业务处理
//....
//返回ItemsCustom
ItemsCustom itemsCoustom = new ItemsCustom(); //将items的属性值拷贝到itemsCustom
BeanUtils.copyProperties(items, itemsCoustom);
return itemsCoustom;
} @Override
public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {
//添加业务校验,通常在service接口对关键参数进行校验
//校验 id是否为空,如果为空抛出异常 //更新商品信息使用updateByPrimaryKeyWithBLOBs根据id更新items表中所有字段,包括 大文本类型字段
//updateByPrimaryKeyWithBLOBs要求必须转入id
itemsCustom.setId(id);
itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
} }
ItemsController.java:
@Controller
@RequestMapping("/items")
public class ItemsController { @Autowired
private ItemsService itemsService; @RequestMapping("/findItems")
public ModelAndView findItems() throws Exception { List<ItemsCustom> itemsList = itemsService.findItemsList(null); ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList", itemsList);
modelAndView.setViewName("items/itemsList");
return modelAndView;
} //商品信息修改页面显示
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
public String editItems(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception{
ItemsCustom itemsCustom = itemsService.findItemsById(1); //通过形参中的model将model数据传到页面
//相当于modelAndView.addObject方法
model.addAttribute("itemsCustom", itemsCustom); return "items/editItems";
} //商品信息修改提交
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(HttpServletRequest request)throws Exception { //重定向到商品查询列表
return "redirect:findItems.action";
//页面转发
//return "forward:findItems.action";
//return "success";
}
}
/springMVC/WebRoot/WEB-INF/jsp/items/editItems.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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> <form id="itemForm" action="${pageContext.request.contextPath }/items/editItemsSubmit.action" method="post" >
<input type="hidden" name="id" value="${itemsCustom.id }"/>
修改商品信息:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td><input type="text" name="name" value="${itemsCustom.name }"/></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="price" value="${itemsCustom.price }"/></td>
</tr>
<tr>
<td>商品生产日期</td>
<td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
</tr>
<%-- <tr>
<td>商品图片</td>
<td>
<c:if test="${item.pic !=null}">
<img src="/pic/${item.pic}" width=100 height=100/>
<br/>
</c:if>
<input type="file" name="pictureFile"/>
</td>
</tr> --%>
<tr>
<td>商品简介</td>
<td>
<textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"/>
</td>
</tr>
</table> </form>
</body> </html>
items/itemsList.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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>
<form action="${pageContext.request.contextPath }/item/findItems.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
二、@RequestMapping:
1)url映射
定义controller方法对应的url,进行处理器映射使用。
2)窄化请求映射
就像上面项目中的在ItemsContorller的类头上加上@RequestMapping("/items"),所有的方法的url路径就加上了/items;
3)限制http请求方法
@RequestMapping(value="/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返回值)的更多相关文章
- SpringMVC Controller 返回值几种类型
SpringMVC Controller 返回值几种类型 2016年06月21日 19:31:14 为who而生 阅读数:4189 标签: Controller 返回值类型spring mvc 更多 ...
- WPF-学习笔记 动态修改控件Margin的值
原文:WPF-学习笔记 动态修改控件Margin的值 举例说明:动态添加一个TextBox到Grid中,并设置它的Margin: TextBox text = new TextBox(); t_gri ...
- Spring MVC controller返回值类型
SpringMVC controller返回值类型: 1 String return "user":将请求转发到user.jsp(forword) return "red ...
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
- SpringMVC由浅入深day01_9商品修改功能开发
9 商品修改功能开发 9.1 需求 操作流程: 1.进入商品查询列表页面 2.点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询) 要修改的商品从数据库查询,根据商品id(主键)查询商 ...
- JAVAEE——SpringMVC第二天:高级参数绑定、@RequestMapping、方法返回值、异常处理、图片上传、Json交互、实现RESTful、拦截器
1. 课前回顾 https://www.cnblogs.com/xieyupeng/p/9093661.html 2. 课程计划 1.高级参数绑定 a) 数组类型的参数绑定 b) List类型的绑定 ...
- SpringMVC学习笔记:数据的接收与返回
SpringMVC的定义:Spring Web MVC is the original web framework built on the Servlet API and included in t ...
- springMVC对于Controller返回值的可选类型
2018-01-11 对于springMVC处理方法支持支持一系列的返回方式: (1)ModelAndView (2)Model (3)ModelMap (4)Map (5)View (6)Stri ...
- SpringMVC Controller 返回值的可选类型
spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...
随机推荐
- bzoj3105
题解: 一道博弈论 题目要求取得最少,那么就是留下的最多 把石子从大到小排序 从打的开始刘 如果可以留,那么就留下了 如果留下了与前面留下来的异或后不为0,那么就可以留 代码: #include< ...
- TMemo Ctrl + A
http://delphi.about.com/od/adptips2004/a/bltip0804_4.htm Here's how to implement the code for the CT ...
- Flask初级(十)flash与前台交互post详解
Project name :Flask_Plan templates:templates static:static POST提交方式,首先要有表单 老实去改模板文件吧. 查询窗口我准备放在页面最顶上 ...
- Kafka生产者APi
kafka客户端发布record(消息)到kafka集群. 新的生产者是线程安全的,在线程之间共享单个生产者实例,通常单例比多个实例要快. 一个简单的例子,使用producer发送一个有序的key/v ...
- windows内存体系结构 内存查询,读,写(附录源码)
“进程内存管理器”这个程序实现的最基本功能也就是对内存的读写,之前的两篇文章也就是做的一个铺垫,介绍了内核模式切换和IoDeviceControl函数进行的应用程序与驱动程序通信的问题.接下来就进入正 ...
- 【Grails 代理Proxy设置】部署Grails遇到Error Resolve error obtaining dependencies:错误,及解决方法
最近在使用Grails,一开始使用3.0.2版本,一直包nullpointer错误,后来使用了2.5.0版本,可以创建工程,但是在进入到工程目录后,再执行grails程序,报错了,步骤如下: 1. g ...
- js方法参数问题
大家都知道java是强类型语言,而js是弱类型语言,其实,它们之间的区别不止这一点,还有方法参数及调用问题,参看下例: js中:这里定义了一个query()方法 function query() { ...
- c语言基础:数据类型 分类: iOS学习 c语言基础 2015-06-10 21:43 9人阅读 评论(0) 收藏
C语言基本数据类型大体上分为: 整型 和 浮点型 字节: 计算机中最小的储存单位 1 Byte = 8 bit 整型: int 4 ...
- 【opencv基础】Rect类的神奇用法
前言 最近看github上源码发现对两个cv::Rect使用相与(&)操作,猛地感觉自己蒙啦,Rect类还有这种神奇用法?!翻看opencv官网Rect类,果然如此! opencv中Rect类 ...
- 配置海康相机SDK文件
前言 项目使用到海康摄像机,进行二次开发需要首先对SDK文件进行相关配置. 实现过程 1.下载SDK开发包: 网址:http://www.hikvision.com/cn/download_61.ht ...