/**
* SpringMVC数据格式化,在所需要转换的字段上面使用
* @xxxformat注解,在pattern中写上格式即可自动转换为对应的类型
* 如:@DateTimeFormat
* @NumberFormat
* 参考FormatterVO 类
*
* @param vo
* @return
*/
springmvc.xml
* 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>