数据绑定之数据类型转换

springmvc已经创建好了数据转换和数据绑定的类

如果我们的数据不符合他们定义好的格式,需要自己处理

处理方式两种

3.1 方式1 自定义转换类
3.1.1 创建转化类 extends PropertyEditorSupport

DateEditor.java

package com.test.binder;

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

//自定义转换类
public class DateEditor extends PropertyEditorSupport {

    private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public void setAsText(String text) throws IllegalArgumentException {

        Date date=null;


        try {
            date= simpleDateFormat.parse(text);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        setValue(date);  //将数据类型转换后的值 存入 对象的属性中

    }
}

FloatEditor.java

package com.test.binder;

import java.beans.PropertyEditorSupport;

public class FloatEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {

        // text  1,234,567  //金额
        //去掉,
        text=text.replaceAll(",","");

        float value= Float.parseFloat(text);

        setValue(value);
    }
}
1.2 注册到WebDataBinder

​ 在控制器中注册

@Controller
@RequestMapping("/card")
public class CardController {

    @InitBinder
    public void initBinder(WebDataBinder binder){

        //注册自定义的绑定对象
        binder.registerCustomEditor(Date.class,new DateEditor());
        //如果对象属性是float  这里用float.class
        //如果对象属性是Float 这里要用Float.class
        binder.registerCustomEditor(float.class,new FloatEditor());
    }
    }

3.1.3 测试案例

package com.test.pojo;

import java.util.Date;

public class CardInfo {

    private String cardID;
    private String uname;
    private float money;
    private Date createTime;

    public String getCardID() {
        return cardID;
    }

    public void setCardID(String cardID) {
        this.cardID = cardID;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public float getMoney() {
        return money;
    }

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

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "CardInfo{" +
                "cardID='" + cardID + '\'' +
                ", uname='" + uname + '\'' +
                ", money=" + money +
                ", createTime=" + createTime +
                '}';
    }
}


<form action="card/addCard" method="post">
    卡号:<input type="text" name="cardID" />
    用户名:<input type="text" name="uname" />
    余额:<input type="text" name="money" />
    开始使用日期:<input type="text" name="createTime" />
    <input type="submit" name="sub" value="提交" />

</form>
package com.test.controller;

import com.test.binder.DateEditor;
import com.test.binder.FloatEditor;
import com.test.pojo.CardInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
@RequestMapping("/card")
public class CardController {

    @InitBinder
    public void initBinder(WebDataBinder binder){

        //注册自定义的绑定对象
        binder.registerCustomEditor(Date.class,new DateEditor());
        //如果对象属性是float  这里用float.class
        //如果对象属性是Float 这里要用Float.class
        binder.registerCustomEditor(float.class,new FloatEditor());
    }

    @RequestMapping("/addCard")
    public String addCard(CardInfo cardInfo)
    {
           System.out.println("---------");
           System.out.println(cardInfo.getCardID());
           System.out.println(cardInfo);
           return "success";
    }


}

3.2 通过注解的方式
3.2.1 设置配置信息,开启注解

​ springmvc.xml文件中添加

mvc:annotation-driven会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的,即解决了@Controller注解使用的前提配置。

同时它还提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB,读写JSON的支持(Jackson)。我们处理响应ajax请求时,就使用到了对json的支持(配置之后,在加入了jackson的core和mapper包之后,不写配置文件也能自动转换成json)。

   <mvc:annotation-driven></mvc:annotation-driven> 

3.2.2 在类CardInfo2类的属性中添加注解

//通过注解来实现数据类型的转换
public class CardInfo2 {

    private String cardID;
    private String uname;
    
    @NumberFormat(pattern ="#,#.#" )
    private float money;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTime;

    public String getCardID() {
        return cardID;
    }

    public void setCardID(String cardID) {
        this.cardID = cardID;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public float getMoney() {
        return money;
    }

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

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "CardInfo{" +
                "cardID='" + cardID + '\'' +
                ", uname='" + uname + '\'' +
                ", money=" + money +
                ", createTime=" + createTime +
                '}';
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值