javaee springMVC日期类型转换之通过注解的方式

spring配置文件

在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>

jsp

html 复制代码
<%--
  Created by IntelliJ IDEA.
  User: HIAPAD
  Date: 2019/11/29
  Time: 14:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="users/addUser" method="post">

    <pre>
    用户名:<input type="text" name="uname" />
    密码:<input type="password" name="pwd" />
    地址:<input type="text" name="address.detail" />
        出生:<input type="text" name="birthday" />
    <input type="submit" name="sub" value="提交" />

    </pre>
</form>


</body>
</html>

po

在属性上添加注解

@DateTimeFormat(pattern = "yyyy-MM-dd")

private Date birthday;

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

import org.springframework.format.annotation.DateTimeFormat;

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;

    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 +
                '}';
    }
}
相关推荐
半兽先生18 分钟前
WebRtc 视频流卡顿黑屏解决方案
java·前端·webrtc
南星沐1 小时前
Spring Boot 常用依赖介绍
java·前端·spring boot
代码不停2 小时前
Java中的异常
java·开发语言
何似在人间5753 小时前
多级缓存模型设计
java·jvm·redis·缓存
多云的夏天3 小时前
ubuntu24.04-MyEclipse的项目导入到 IDEA中
java·intellij-idea·myeclipse
Fanxt_Ja3 小时前
【数据结构】红黑树超详解 ---一篇通关红黑树原理(含源码解析+动态构建红黑树)
java·数据结构·算法·红黑树
Aphelios3803 小时前
TaskFlow开发日记 #1 - 原生JS实现智能Todo组件
java·开发语言·前端·javascript·ecmascript·todo
weixin_448771723 小时前
使用xml模板导出excel
xml·java·excel
烁3474 小时前
每日一题(小白)模拟娱乐篇27
java·数据结构·算法·娱乐
魔道不误砍柴功5 小时前
2025年Java无服务器架构实战:AWS Lambda与Spring Cloud Function深度整合
java·架构·serverless