SpringMVC数据格式化标签

本文介绍如何在SpringMVC中使用@DateTimeFormat和@NumberFormat注解进行日期和数值的格式化处理,通过示例代码展示了具体实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在SpringMVC配置文件中配置
2.在目标POJO对象的属性上加上@NumberFormat 或者 @DateTimeFormat 注解!
@DateTimeFormat
– pattern 属性:类型为字符串。指定解析/格式化字段数据的模式, 如:”yyyy-MM-dd hh:mm:ss”
@NumberFormat
–pattern:类型为 String,自定义样式,金钱1,000,989,222 如patter=”#,###,###,###”;
在form表单 提交后,SpringMVC自动封装成POJO类
示例代码:

package com.ysu.bean;

import java.util.Date;

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

public class Student {
    private String id;
    private String name;
    @DateTimeFormat(pattern="yyy-MM-dd")
    private Date brith;
    @NumberFormat(pattern="###,###")
    private int salary;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBrith() {
        return brith;
    }
    public void setBrith(Date brith) {
        this.brith = brith;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
}

controller代码

@RequestMapping(value="/toStudent",method=RequestMethod.POST)
    public String toStudent(@ModelAttribute(value="student")Student stu1,ModelMap map){
        //map.addAttribute("student", stu);
        map.addAttribute("student1", stu1);
        return "list";
    }

form代码:

<form method="post" action="${pageContext.request.contextPath }/toStudent">
    学生id<input type="text" name="id"><br>
    学生姓名<input type="text" name="name"><br>
    学生生日<input type="text" name="brith"><br>
    学生学费<input type="text" name="salary"><br>
    <input type="submit" value="提交">
</form>

显示jsp代码

${student1.name}${student1.salary}${student1.brith}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值