SpringMVC-时间类型转换

在上一篇SpringMVC的提交表单中,我们使用的日期为String型,可以将日期转换为Date型,然后使用initBinder函数进行显示,具体代码如下:

(1)首先更改User.java的birthday为Date型

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
package com.zk.domain;
 
import java.util.Date;
 
public class User {
    private Integer id;
    private String name;
    private String age;
    private Date birthday;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

(2)其次,在CommandController.java中添加initBinder方法。

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
package com.zk.UserController;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
 
import com.zk.domain.User;
 
//从页面接受参数,封装javaBean的User对象
public class CommandController extends AbstractCommandController{
    //指定把参数封装到那个对象。
    public CommandController(){
        this.setCommandClass(User.class);
    }
    @Override
    protected ModelAndView handle(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {
        // TODO Auto-generated method stub
        //值被封装命令对象
        User user=(User) command;
         
        ModelAndView mv=new ModelAndView();
        //设置models
        mv.addObject("user",user);
        //指定返回视图界面
        mv.setViewName("index");
         
        return mv;
    }
    @Override
    protected void initBinder(HttpServletRequest request,
            ServletRequestDataBinder binder) throws Exception {
        // TODO Auto-generated method stub
        String birthday=request.getParameter("birthday");
        if(birthday.contains("/")){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"),true));
        }else{
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
        }
    }
     
}

  其余与SpringMVC提交表单的代码相同,运行后得到如下运行结果:

 

posted @   leagueandlegends  阅读(588)  评论(1编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示