33、(知识篇)SpringMVC10 数据格式化

/**
* SpringMVC数据格式化,在所需要转换的字段上面使用
* @xxxformat注解,在pattern中写上格式即可自动转换为对应的类型
* 如:@DateTimeFormat
* @NumberFormat
* 参考FormatterVO 类

* @param vo
* @return
*/


测试类:

package com.spring.test;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.spring.vo.FormatterVO;

@Controller
public class TestController {
	
	@RequestMapping("test")
	public String test(Map<String,Object> params) {
		params.put("msg", "传值");
		return "index";
	}
	
	
	/**
	 * SpringMVC数据格式化,在所需要转换的字段上面使用
	 * @xxxformat注解,在pattern中写上格式即可自动转换为对应的类型
	 * 如:@DateTimeFormat
	 * @NumberFormat
	 * 参考FormatterVO 类
	 * 
	 * @param vo
	 * @return
	 */
	
	//测试地址:http://127.0.0.1:8080/33Spring10/testFormat?beginDate=2017-01-01&price=123,456.75
	//结果会自动转为:vo == FormatterVO [beginDate=Sun Jan 01 00:00:00 CST 2017, price=123456.75]
	@RequestMapping("testFormat")
	public String testFormat(FormatterVO vo){
		System.out.println("vo == "+vo);
		return "index";
	}
}


格式化类FormatterVO

package com.spring.vo;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;

public class FormatterVO {
	
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date beginDate;
	@NumberFormat(pattern="#,###,###.##")
	private double price;
	public FormatterVO() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public FormatterVO(Date beginDate, double price) {
		super();
		this.beginDate = beginDate;
		this.price = price;
	}

	public Date getBeginDate() {
		return beginDate;
	}
	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "FormatterVO [beginDate=" + beginDate + ", price=" + price + "]";
	}
	
	
}

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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	
	<context:component-scan base-package="com.spring"></context:component-scan>
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven></mvc:annotation-driven>
	
</beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值