SpringMVC 和 mybatis 整合之修改(三)

在上一篇 商品查询 的基础上,对商品添加修改功能。



productSevice 接口:



productServiceImpl 实现类:



ProductsMapper.xml:




参数绑定:

Controller 类:
package lxf.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import lxf.po.Products;
import lxf.po.ProductsCustom;
import lxf.service.ProductsService;

@Controller
//为了便于 url 进行分类管理,可以在类名上加 @RequestMapping来定义根路径,最终得访问地址是: 根路径 + 子路径
//比如:商品列表查询: /Products/queryProducts.action
@RequestMapping("Products")
public class ProductsController {

	@Autowired
	private ProductsService productsService;
	
	@RequestMapping("editProducts")
	public String queryProductsById(Model model,@RequestParam("id") Integer pid){
		/* 简单类型参数绑定:
		 * @RequestParam里面指定 request 传入参数名称和 形参绑定,如果不使用@RequestParam,则传参名称要和形参相同
		 *@RequestParam(value="id",required=  , defaultValue=  )
		 *required 属性指定参数是否必须传入
		 *defaultValue 可以设置默认值,如果参数没有传入,按默认值和形参绑定。*/
		
		//通过形参中的model将model数据传到页面
		//相当于modelAndView.addObject方法
		model.addAttribute("product", productsService.queryProductsById(pid));
		return "editProducts";
	}
	
	@RequestMapping("updateProducts")
	// pojo 参数绑定:
	public String update(Integer id, Products product){
		
		productsService.updateProducts(id, product);
		
		//通过 forward 页面转发,url 地址不变request 可以共享
		//return "forward:queryProducts.action";
		//通过 redirect 重定向
		return "redirect:queryProducts.action";
	}
	
	@RequestMapping("queryProducts")
	public ModelAndView queryProductsList(){
		List<ProductsCustom> proList = productsService.queryProductsList(null);
		
		ModelAndView modelv = new ModelAndView();
		modelv.setViewName("productsList");
		modelv.addObject("proList", proList);
		
		return modelv;
	}
}

pojo 绑定:

下面前端页面的 edit 编辑页面中 <input> 里的 name  和  pojo 属性名称一致,将页面中的数据绑定到 pojo 中。


                                    ||

web.xml :

在上篇  商品查询  的基础上 添加:



前端页面:

productsList.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/queryItem.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="${proList }" var="pro">
<tr>
	<td width="18%">${pro.name }</td>
	<td width="18%">${pro.price }</td>
	<td width="18%"><fmt:formatDate value="${pro.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
	<td>${pro.detail }</td>
	
	<td><a href="${pageContext.request.contextPath }/Products/editProducts.action?id=${pro.id}">修改</a></td>

</tr>
</c:forEach>

</table>
</form>
</body>

</html>


editProducts.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="productsForm" action="${pageContext.request.contextPath }/Products/updateProducts.action" method="post" >
<input type="hidden" name="id" value="${product.id }"/>
修改商品信息:
<table width="100%" border=1>
<tr>
	<td>商品名称</td>
	<td><input type="text" name="name" value="${product.name }"/></td>
</tr>
<tr>
	<td>商品价格</td>
	<td><input type="text" name="price" value="${product.price }"/></td>
</tr>
<tr>
	<td>商品生产日期</td>
 	<td><input type="text" name="createtime" value="<fmt:formatDate value="${product.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">${product.detail }</textarea>
	</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"/>
</td>
</tr>
</table>

</form>
</body>

</html>


自定义参数绑定:

对于 controller 形参中 pojo 对象,如果属性中有日期类型,需要自定义参数绑定。

将请求日期数据串传成 日期类型,要转换的日期类型和 pojo 中日期属性的类型保持一致。


我们需要定义一个转换器。

CustomDateConverter :
package lxf.controller.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public class CustomDateConverter implements Converter<String, Date> {


	@Override
	public Date convert(String source) {
		// 
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		try {
			return sim.parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}

}

然后在 springmvc.xml 中添加:



测试效果:


点击第一个修改


提交:



猜你喜欢

转载自blog.csdn.net/qq_30715329/article/details/80452323
今日推荐