javaee springMVC数字类型转换之通过注解的方式

po

在属性上增加注解 @NumberFormat(pattern = "#,#.#")

java 复制代码
package com.test.pojo;

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

import java.util.Date;

public class Users {

    private int uid;

    private String uname;

    private String pwd;

    //地址对象
    private Address address;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;

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

    public float getMoney() {
        return money;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

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

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public Address getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Users{" +
                "uid=" + uid +
                ", uname='" + uname + '\'' +
                ", pwd='" + pwd + '\'' +
                ", address=" + address +
                ", birthday=" + birthday +
                ", money=" + money +
                '}';
    }
}

spring配置文件

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

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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 1. 配置  需要扫描的控制层在哪个包  -->
    <context:component-scan base-package="com.test.controller"></context:component-scan>

    <!-- 2 配置 视图解析器 中的 前缀和后缀  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 设置前缀  -->
        <property name="prefix" value="/WEB-INF/"/>
        <!-- 设置后缀 -->

        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 开启注解驱动
      注册了两个数据转换的注解@NumberFormatannotation支持,@DateTimeFormat
      json相关的。。
    -->
    <mvc:annotation-driven></mvc:annotation-driven>

</beans>
相关推荐
一只学C的小螃蟹16 分钟前
Java的for循环嵌套
java
重生成为码农‍17 分钟前
Java高级Day52-BasicDAO
java·开发语言·windows
Dola_Pan30 分钟前
Linux系统IO-文件描述符详解
java·linux·服务器
PacosonSWJTU37 分钟前
spring揭秘24-springmvc02-5个重要组件
java·spring
编啊编程啊程42 分钟前
一文上手SpringSecuirty【六】
java·spring boot·分布式·spring cloud
掘金-我是哪吒44 分钟前
springboot第74集:设计模式
java·spring boot·后端·spring·设计模式
matrixlzp1 小时前
IDEA 高版本创建 Spring Boot 项目选不到 java 8
java·java-ee·intellij-idea
Satan7121 小时前
【Java】六大设计原则和23种设计模式
java·开发语言·intellij-idea
kong79069281 小时前
SpringCloud入门(九)Feign实战应用和性能优化
java·spring cloud·feign远程调用