Srpingmvc转换器.String转换Date

package com.itheima.domain;

import java.io.Serializable;
import java.util.Date;

/**
 * 账户的实体类
 * 
 * @author ChonZ
 *
 */
public class Account implements Serializable {
	private String accountId;
	private String name;
	private Float money;
	
	//如果实体类中包含pojo
	private Address address;
	
	//从请求参数中获取Date类型
	private Date date;
		
	public String getAccountId() {
		return accountId;
	}

	public void setAccountId(String accountId) {
		this.accountId = accountId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Float getMoney() {
		return money;
	}

	public void setMoney(Float money) {
		this.money = money;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

}

Account实体类,有Date属性

 

package com.itheima.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.itheima.domain.Account;
import com.itheima.domain.User;

@Controller("accountController")
@RequestMapping("account")
public class AccountController {
			
	/*
	 * account/deleteAccount?date=2018-01-01
	handler方法的形参是Account实体类型,包含Date属性,虽然url的参数的和handler的形参不一致,
	但是springmvc可以查找到Account类中的Date属性,进行封装	
	 */
	@RequestMapping("/deleteAccount")	
	public String deleteAccount(Account account) {
		System.out.println("执行了删除账户" + account.getDate());
		return "success";
	}
}

handler类

 

package com.itheima.web.converter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

/**
 * 自定义类型转换器String转换成Date
 * 
 * @author ChonZ
 *
 */

public class StringToDateConverter implements Converter<String, Date> {

	@Override
	public Date convert(String source) {
		DateFormat format = null;
		//传入的参数为空,无法转换
		if (StringUtils.isEmpty(source)) {
			throw new NullPointerException("请传入正确的数据");
		}
		//创建指定格式的日期转换对象
		format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = format.parse(source);
			//返回日期对象
			return date;
		
		} catch (Exception e) {
			throw new RuntimeException("请输入正确的日期格式");
		}
	}
}

手写转换器类(String转换成Date类)

 

<%@ 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>springmvc的入门案例</title>
</head>
<body>

		
	<!-- 6.类型转换 -->
	<a href="account/deleteAccount?date=2018-01-01">查询用户</a>
</body>
</html>

 

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<!-- 告知spring创建容器时要扫描的包 -->
	<context:component-scan base-package="com.itheima"/>

	<!-- 配置springmvc的视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 配置自定义类型转换器,它的配置方式是采用注册服务的方式来实现的 -->
	<bean id="converterService" class="org.springframework.context.support.ConversionServiceFactoryBean">
		<property name="converters">
			<set>
				<bean class="com.itheima.web.converter.StringToDateConverter"></bean>
			</set>
		</property>
	</bean>
	
	<!-- 配置spring mvc的注解驱动 -->	
	<mvc:annotation-driven conversion-service="converterService"></mvc:annotation-driven>
	
	<!-- 设置静态资源不参与springmvc编码的过滤 -->
	<mvc:resources location="/css/" mapping="/css/**"/>
	<mvc:resources location="/images/" mapping="/images/**"/>
	<mvc:resources location="/scripts/" mapping="/javascripts/**"/>
	
	</beans>

springmvc注册转换器

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值