数据绑定之数据类型转换

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

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

处理方式两种

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

DateEditor.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 
​ 在控制器中注册
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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 测试案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 +
                '}';
    }
}

  

1
2
3
4
5
6
7
8
<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>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)。

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

  

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//通过注解来实现数据类型的转换
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 +
                '}';
    }
}

  

posted @   呆萌老师  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示