Java Spring MVC RequestParam长类型绑定始终为空
在我的一个操作中,我将一个长(非原语)类型绑定到一个请求参数Java Spring MVC RequestParam长类型绑定始终为空,java,spring,spring-mvc,Java,Spring,Spring Mvc,在我的一个操作中,我将一个长(非原语)类型绑定到一个请求参数 @RequestMapping("/history") public ModelAndView historyList_GET( @RequestParam(value="startDate",required=false) Long startDate, @RequestParam(value="endDate",required=false) Long endDate
@RequestMapping("/history")
public ModelAndView historyList_GET(
@RequestParam(value="startDate",required=false) Long startDate,
@RequestParam(value="endDate",required=false) Long endDate
)
{
}
但是这些变量的值总是空的。所以我检查了文档,搜索了一个类似的问题,但什么也没找到
当然,我可以将绑定类型更改为String,然后将其转换为Long,但在我看来,这不是一个好的解决方案。这只是一个解决办法
我看到的另一种方式是,人们使用包装器对象将其与@modeldattribute注释绑定。例如,
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;
}
}
您一定错过了什么,您提供的代码运行良好,参数也按预期绑定。您确定在发送请求时执行此特定方法吗?顺便说一句,
@modeldattribute
用于稍有不同的情况。我确信,在给出请求时会执行这种方法。至于我遗漏的部分,我也考虑过,但是,这是一个非常简单的方法,它应该绑定它。。。我不明白为什么。你们能在公共存储库中重现这个问题吗?