• 接收的参数为日期类型


    当网页端传来的参数是日期格式类型时解决方法

    1. 在controller中加入如下方法

     1 package com.zhiyou100.mcl.controller;
     2 
     3 import java.text.SimpleDateFormat;
     4 import java.util.Date;
     5 import java.util.Map;
     6 
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpSession;
     9 
    10 import org.springframework.beans.propertyeditors.CustomDateEditor;
    11 import org.springframework.http.HttpRequest;
    12 import org.springframework.stereotype.Controller;
    13 import org.springframework.ui.Model;
    14 import org.springframework.web.bind.ServletRequestDataBinder;
    15 import org.springframework.web.bind.annotation.InitBinder;
    16 import org.springframework.web.bind.annotation.RequestMapping;
    17 import org.springframework.web.bind.annotation.SessionAttributes;
    18 import org.springframework.web.servlet.ModelAndView;
    19 
    20 import com.zhiyou100.mcl.bean.Users;
    21 
    22 
    23 
    24 @Controller
    25 @RequestMapping("user")
    26 public class UserController {
    27         @RequestMapping("list.do")
    28     public String todate(Date todate) {
    29         System.out.println(todate);        
    30         return "list";        
    31     }
    32 }
    33 
    34 //加入代码
    35 @InitBinder
    36     public void initBinder(ServletRequestDataBinder binder){
    37         //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型,
    38         binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),//如果需要也可以写成  yyyy-MM-dd  HH:mm:ss
    39                 true));
    40     }
    41     

    2.controller中用实体接受页面传递的参数

    1 @DateTimeFormat(pattern="yyyy-MM-dd")
    2     private Date date;

    在实体类日期类型上一行加上   @DateTimeFormat(pattern="yyyy-MM-dd")

  • 相关阅读:
    SharePoint 2010 世界(一)
    joomla个性定制(五)
    express框架简析🐱‍🏍
    组件
    日常
    mongo数据库浅析
    vue浅析
    react开发环境
    jsonserver
    关于arraylist.remove的一些小问题。
  • 原文地址:https://www.cnblogs.com/mcl2238973568/p/11455293.html
Copyright © 2020-2023  润新知