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


    spring配置文件

    在spring配置文件中增加mvc:annotation-driven

    
    <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">
        
        <context:component-scan base-package="com.test.controller">context:component-scan>
    
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            
            <property name="prefix" value="/WEB-INF/"/>
            
    
            <property name="suffix" value=".jsp"/>
        bean>
        
        <mvc:annotation-driven>mvc:annotation-driven>
    
    beans>
    
    • 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

    jsp

    <%--
      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>Titletitle>
    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>
    
    
    • 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

    po

    在属性上添加注解
    @DateTimeFormat(pattern = “yyyy-MM-dd”)
    private Date birthday;

    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 +
                    '}';
        }
    }
    
    
    • 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
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
  • 相关阅读:
    刷题记录:牛客NC50493石子合并
    HTTPS的理解(证书、认证方式、TLS握手)
    【scikit-learn基础】--『监督学习』之 贝叶斯分类
    文件上传之图片码混淆绕过(upload的16,17关)
    电商API按关键字搜索商品
    AI调度程序突然很多推送AI任务失败----是redis的锅吗
    java毕业设计宠物喂养资讯分享平台的设计与实现Mybatis+系统+数据库+调试部署
    Linux--进程概念
    python: 用百度API读取增值税发票信息
    Android Process管理(进程管理) 详解
  • 原文地址:https://blog.csdn.net/Rockandrollman/article/details/132779611