我这样做的方法是使用自定义 QueryStringBindable . 这样我在路线中表达参数:
GET /birthdays/ controllers.Birthdays.getBirthdays(period: util.Period)
Period的代码如下所示 .
public class Period implements QueryStringBindable {
public static final String PATTERN = "dd.MM.yyyy";
public Date start;
public Date end;
@Override
public F.Option bind(String key, Map data) {
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);
try {
start = data.containsKey("startDate")?sdf.parse(data.get("startDate") [0]):null;
end = data.containsKey("endDate")?sdf.parse(data.get("endDate")[0]):null;
} catch (ParseException ignored) {
return F.Option.None();
}
return F.Option.Some(this);
}
@Override
public String unbind(String key) {
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);
return "startDate=" + sdf.format(start) + "&" + "endDate=" + sdf.format(end);
}
@Override
public String javascriptUnbind() {
return null;
}
public void applyDateFilter(ExpressionList el) {
if (this.start != null)
el.ge("eventDate", this.start);
if (this.end != null)
el.le("eventDate", new DateTime(this.end.getTime()).plusDays(1).toDate());
}
}
如果我想对查询应用日期过滤, applyDateFilter 只是我在控制器中使用的一种便利方法 . 显然,您可以在此处使用其他日期默认值,或者在 bind 方法中使用除null之外的其他默认值作为开始日期和结束日期 .
本文介绍了一种使用自定义QueryStringBindable实现日期过滤的方法。通过在GET请求中传递开始和结束日期参数,可以灵活地对查询结果进行时间范围筛选。
85

被折叠的 条评论
为什么被折叠?



