繁体   English   中英

Spring MVC RequestParam长类型绑定始终为空

[英]Spring MVC RequestParam Long Type Binding Always Null

在执行的一项操作中,我已将Long(不是基本类型)类型绑定到请求参数。

@RequestMapping("/history")
    public ModelAndView historyList_GET(
            @RequestParam(value="startDate",required=false) Long startDate,
            @RequestParam(value="endDate",required=false) Long endDate
            )
    {
    }

但是这些变量的值始终为空。 因此,我检查了文档,搜索了类似的问题,但没有发现任何问题。

当然,我可以将绑定类型更改为String,然后将其转换为Long,但是我认为这不是一个好的解决方案。 只是解决方法...

我看到的另一种方式是,人们使用包装对象将其与@ModelAttribute批注绑定。 如;

public class Wrapper
{
 public Long startDate;
 public Long endDate;
}

@RequestMapping("/history")
    public ModelAndView historyList_GET(
            @ModelAttribute Wrapper dates
            )
{
}

但是同样,这也是一种解决方法。

我问这怎么可能? 为什么即使所有其他引用类型都能完美绑定,但Long却没有呢? 是因为我错过了什么吗?

这些是顺便提一下的要求...

/history?endDate=144656539476
/history?startDate=144656539476
/history?startDate=144656539476&endDate=14499999999

您的包装器类缺少setter方法。

对我有用的包装器类的实际代码:-

public class Wrapper {
    public Long startDate;
    public Long endDate;

    public Long getStartDate() {
        return startDate;
    }

    public void setStartDate(Long startDate) {
        this.startDate = startDate;
    }

    public Long getEndDate() {
        return endDate;
    }

    public void setEndDate(Long endDate) {
        this.endDate = endDate;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM