• SpringMVC-时间类型转换


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

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

    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方法。

    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提交表单的代码相同,运行后得到如下运行结果:

  • 相关阅读:
    Python 3基础教程1-环境安装和运行环境
    Python+Selenium框架设计篇之-什么是POM
    CS/BS架构的特点
    HTTP 方法:GET 对比 POST
    apt的通讯信道是如何发现的?
    安全运维到运营
    僵尸网络是如何控制你的电脑的?
    了解资产,减少网络攻击面的威胁
    ISC安全大会感受
    mysql 在忘记密码不能登录情况下的设置方法
  • 原文地址:https://www.cnblogs.com/longlyseul/p/11409832.html
Copyright © 2020-2023  润新知