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 +
                '}';
    }
}
相关推荐
Otaku love travel4 分钟前
老系统改造增加初始化,自动化数据源配置(tomcat+jsp+springmvc)
java·tomcat·初始化·动态数据源
DKPT17 分钟前
Java设计模式之行为型模式(责任链模式)介绍与说明
java·笔记·学习·观察者模式·设计模式
L_autinue_Star32 分钟前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
晨岳1 小时前
CentOS 安装 JDK+ NGINX+ Tomcat + Redis + MySQL搭建项目环境
java·redis·mysql·nginx·centos·tomcat
执笔诉情殇〆1 小时前
前后端分离(java) 和 Nginx在服务器上的完整部署方案(redis、minio)
java·服务器·redis·nginx·minio
YuTaoShao1 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
程序员的世界你不懂1 小时前
(20)Java+Playwright自动化测试- 操作鼠标拖拽 - 上篇
java·python·计算机外设
AI360labs_atyun2 小时前
Java在AI时代的演进与应用:一个务实的视角
java·开发语言·人工智能·科技·学习·ai
不像程序员的程序媛2 小时前
redis的一些疑问
java·redis·mybatis
知其然亦知其所以然3 小时前
Java 面试高频题:GC 到底回收了什么、怎么回收、啥时候回收?
java·后端·面试