zoukankan      html  css  js  c++  java
  • 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提交表单的代码相同,运行后得到如下运行结果:

  • 相关阅读:
    路由守卫
    this.$nextTick() 的一些理解
    3d饼图
    element ui 可编辑的表格
    vue 路由传参
    vue+element ui 时间格式化
    element ui 选择期 传对象
    数据结构学习第十天
    数据结构学习第九天
    数据结构学习第八天
  • 原文地址:https://www.cnblogs.com/longlyseul/p/11409832.html
Copyright © 2011-2022 走看看