SpringMVC的自定义参数绑定

本文介绍如何在SpringMVC框架中整合MyBatis并自定义日期参数格式,通过自定义参数绑定解决日期数据多种格式的问题,实现商品修改页面中日期字段的灵活设置。

SpringMVC整合MyBatis
需求:在商品修改页面可以修改商品的生产日期,并且根据业务需求自定义日期格式。如将下面的商品列表中背包的日期修改为"2018-11-21 07:20:22"。
在这里插入图片描述
分析:由于日期数据有很多种格式,springmvc没办法把字符串转换成日期类型。所以需要自定义参数绑定。
前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestMapping标记的方法进行适配,并对方法中的形参进行参数绑定。可以在springmvc处理器适配器上自定义转换器Converter进行参数绑定。
一般使用 < mvc:annotation-driven/>注解驱动加载处理器适配器,可以在此标签上进行配置。
工程架构如下:
在这里插入图片描述
1 DateConverter.java自定义class如下:

package com.sea.ssm.controller;

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

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

/**
 * @author Sea You
 *
 * 2018年11月24日下午10:19:15
 */
public class DateConverter implements Converter<String, Date> {

	@Override
	public Date convert(String dateStr) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = dateFormat.parse(dateStr);
			return date;
		} catch (ParseException e) {

			e.printStackTrace();
		}
		return null;
	}

}

2 springmvc.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<!-- 配置controller扫描包 -->
	<context:component-scan base-package="com.sea.ssm.controller" />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置逻辑视图的前缀 -->
		<property name="prefix" value="/WEB-INF/jsp/" />
		<!-- 配置逻辑视图的后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>
	
	<mvc:annotation-driven conversion-service="conversionService" />
	<!-- 配置自定义日期类型转换器 -->
	<bean id="conversionService"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<set>
				<bean class="com.sea.ssm.controller.DateConverter"></bean>
			</set>
		</property>
	</bean>
</beans>

3` ItemServiceImpl实现如下:

package com.sea.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sea.ssm.mapper.ItemMapper;
import com.sea.ssm.pojo.Item;
import com.sea.ssm.pojo.ItemExample;
import com.sea.ssm.pojo.ItemExample.Criteria;
import com.sea.ssm.service.ItemService;
import com.sea.ssm.util.pojo.QueryVo;

@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private ItemMapper itemMapper;

	@Override
	public List<Item> queryItemList() {
		ItemExample example = new ItemExample();
		List<Item> list = itemMapper.selectByExample(example);
		return list;
	}

	@Override
	public Item queryById(int itemId) {
		Item item = null;
		item = itemMapper.selectByPrimaryKey(itemId);
		return item;
	}

	@Override
	public int updateItem(Item item) {
		int result = -1;
		if (item == null)
			return result;

		Item editItem = itemMapper.selectByPrimaryKey(item.getId());
		if (editItem == null)
			return result;

		editItem.setDetail(item.getDetail());
		editItem.setName(item.getName());
		editItem.setPrice(item.getPrice());
		editItem.setCreatetime(item.getCreatetime());
		result = itemMapper.updateByPrimaryKey(editItem);
		return result;
	}

	@Override
	public List<Item> queryByVo(QueryVo vo) {
		ItemExample example = new ItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(vo.getItem().getId());
		criteria.andNameLike("%"+vo.getItem().getName()+"%");
		return itemMapper.selectByExample(example);
	}

}

4 ItemController.java如下:

package com.sea.ssm.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

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

import com.sea.ssm.pojo.Item;
import com.sea.ssm.service.ItemService;
import com.sea.ssm.util.pojo.QueryVo;

@Controller
public class ItemController {
	@Autowired
	private ItemService itemService;

	@RequestMapping("/itemList")
	public ModelAndView itemList() {
		List<Item> list = itemService.queryItemList();
		ModelAndView modelAndView = new ModelAndView("itemList");
		modelAndView.addObject("itemList", list);
		return modelAndView;
	}

	@RequestMapping("/itemEdit.action")
	public ModelAndView itemEdit(int id) {
		Item item = itemService.queryById(id);
		ModelAndView modelAndView = new ModelAndView("editItem");
		modelAndView.addObject("item", item);
		return modelAndView;
	}

	@RequestMapping("/updateitem.action")
	public void updateItem(HttpServletResponse response, Item item) throws IOException {
		System.out.println(item);
		itemService.updateItem(item);
		response.sendRedirect("itemList.action");
	}
	@RequestMapping("/queryItem")
	public ModelAndView queryVo(QueryVo vo){
		System.out.println(vo.getItem().getId());
		System.out.println(vo.getItem().getName());
		List<Item> itemList = itemService.queryByVo(vo);
		ModelAndView modelAndView = new ModelAndView("itemList");
		modelAndView.addObject("itemList", itemList);
		return modelAndView;
	}
}

效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值