(转)SpringMVC:提交数据遭遇基础类型和日期类型报400错误解决方法

原文地址:http://www.cnblogs.com/morlin/p/4382707.html?utm_source=tuicool&utm_medium=referral
使用SpringMVC开发的时候,页面如果有日期格式的数据,后台接受也是java.util.Date,则报告400错误 。下面是解决方案的演示示例:

这个是实体类,里面createDate就是java.util.Date类型

import java.util.Date;

public class User {

    private int userId;
    private String userName;
    private Date createDate;

    public User() {}

    public User(int userId, String userName, Date createDate) {
        super();
        this.userId = userId;
        this.userName = userName;
        this.createDate = createDate;
    }

    public User(String userName, Date createDate) {
        super();
        this.userName = userName;
        this.createDate = createDate;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    @Override
    public String toString() {
        return "User [createDate=" + createDate + ", userId=" + userId
                + ", userName=" + userName + "]";
    }
}

页面代码

<form action="regUser" method="post">
        userName:<input type="text" name="userName"/><br>
        createDate:<input type="text" name="createDate"/><br>
        double类型:<input type="text" name="dd"/><br>
        <input type="submit" value="注册">
    </form>

因为对于原生基本类型的form表单绑定,会出错。需要指定具体的类型编辑器。用法如下:首先在BaseController中增加方法initBinder,并使用注解@InitBinder标注,那么spring mvc在绑定表单之前,都会先注册这些编辑器。剩下的控制器都继承该类。CustomDateEditor spring自己已经提供了。代码如下:

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import sun.beans.editors.DoubleEditor;
import sun.beans.editors.FloatEditor;
import sun.beans.editors.IntEditor;
import sun.beans.editors.LongEditor;

@Controller
public class BaseController {

    @InitBinder    
    public void initBinder(WebDataBinder binder) {    

        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); 
        binder.registerCustomEditor(int.class, new IntEditor());
        binder.registerCustomEditor(long.class, new LongEditor());  
        binder.registerCustomEditor(double.class, new DoubleEditor());  
        binder.registerCustomEditor(float.class, new FloatEditor());  
    }


}

上面的代码不仅仅有日期格式的编辑器,还有基础类型的编辑器,这样就解决了SpringMVC中controller方法接受参数的时候,基础类型报错的问题了。

下面是测试用代码,继承BaseController之后就可以直接运行了。接受的参数有实体类和基础类型。

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kickstarter.entity.User;

@Controller("userController")
public class UserController extends BaseController{

    @RequestMapping(value="regUser")
    public String dateTest(User user , double dd){

        System.out.println( user.toString() );
        System.out.println( dd );
        return "index"; 
    }
}

以上,问题解决。然后我们切换第二种方式,删除 BaseController 这个类,直接在User实体类中的 createDate字段上加上注解

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

public class User {

    private int userId;
    private String userName;

    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date createDate;

    public User() {}

    public User(int userId, String userName, Date createDate) {
        super();
        this.userId = userId;
        this.userName = userName;
        this.createDate = createDate;
    }

    public User(String userName, Date createDate) {
        super();
        this.userName = userName;
        this.createDate = createDate;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    @Override
    public String toString() {
        return "User [createDate=" + createDate + ", userId=" + userId
                + ", userName=" + userName + "]";
    }
}

这样也可以解决日期格式报400问题。而且不管页面是否有数据都可以正常使用。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值