zoukankan      html  css  js  c++  java
  • 接收的参数为日期类型

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

    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")

  • 相关阅读:
    Cpp Singleton
    回溯法( Backtracking Algorithms ) :C语言Maze迷宫问题(自己实现)
    First wxWidgets Demo, wxWidgets简单示例
    copy contructor拷贝构造函数 (Copy Control)
    UPX (Ultimate Packer for eXecutables) 多平台可执行文件压缩
    wxWidgets Event Handling
    WxWidgets 安装与测试
    顺序查找 sequential find
    HDU1171 Big Event in HDU 背包
    HDU2091 水题
  • 原文地址:https://www.cnblogs.com/mcl2238973568/p/11455293.html
Copyright © 2011-2022 走看看