Java Spring MVC-将空日期作为参数传递
我正在尝试在我的一个控制器上实现一个过滤器 这是控制器Java Spring MVC-将空日期作为参数传递,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在尝试在我的一个控制器上实现一个过滤器 这是控制器 @RequestMapping(value = "", method = RequestMethod.GET) public String listSpots(ModelMap model, @RequestParam (value= "page", required = false) Integer page, @RequestParam (value="numeroPos
@RequestMapping(value = "", method = RequestMethod.GET)
public String listSpots(ModelMap model, @RequestParam (value= "page", required = false) Integer page,
@RequestParam (value="numeroPosto", required = false) Integer numeroPosto,
@RequestParam(value="nomePosto", required = false) String nomePosto,
@RequestParam(value="occupied", required = false) Integer occupied,
@RequestParam(value="idPark", required = false) Integer idPark,
@RequestParam(value="idPiano", required = false) Integer idPiano,
@RequestParam(value="dateTime", required = false) Date dateTime) {
if (page == null) {
currentPage = 1;
} else {
currentPage = page;
}
int offset = (currentPage - 1) * elementsPerPage;
//creo la mappa dei criteri
Map<String, Object> criteri = new HashMap<String, Object>();
criteri.put("numeroPosto", numeroPosto);
criteri.put("nomePosto", nomePosto);
criteri.put("occupied", occupied);
Park park = null;
Piano piano = null;
if(idPark!=null){
park = parkService.findById(idPark);
}
if(idPiano!=null){
piano = pianoService.findById(idPiano);
}
criteri.put("park", park);
criteri.put("piano", piano);
criteri.put("dateTime", dateTime);
int numOfRows = postoService.showSpotsCount(criteri);
List<Posto> posti = postoService.showSpots(offset, criteri);
List<Posto> posto = new ArrayList<Posto>(posti.size());
for (Posto javaBean : posti){
Date date = new Date();
Date start = new Timestamp(date.getTime());
Date end = javaBean.getDateTime();
DateTime st = new DateTime(start);
DateTime en = new DateTime(end);
Long hours = postoService.getHours(st, en);
Long minutes = postoService.getMinutes(st, en);
javaBean.setHours(hours);
javaBean.setMinutes(minutes);
posto.add(javaBean);
}
int pages = 1+(numOfRows / elementsPerPage);
String pageTitle = messageSource.getMessage("spot.list", null, locale);
model.addAttribute("pageTitle", pageTitle);
model.addAttribute("cssActiveSpots", cssActiveSpots);
model.addAttribute("posto", posto);
model.addAttribute("currentPage", currentPage);
model.addAttribute("pages", pages);
model.addAttribute("numOfRows", numOfRows);
return path + "/posti";
}
我能解决这个问题吗?只要将它添加到控制器中,它就会工作
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// change the format according to your need.
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
你试过跳过这个参数吗?如果我跳过它,打算删除这个参数,一切正常。。。如果我在其中输入了正确的日期,它也可以工作。我不是说从控件中删除。只是不要通过它。您将required设置为false。如果将“”作为日期传递,则会出现错误,因为“”无法转换为日期对象,因为在筛选结果时,我的表单中有它,但它不会出现在每个视图中。我的意思是,如果我跳过required=false,我必须在我向控制器发出的每个请求中设置日期时间,这不是我们需要的。是否有方法分析“”值?您可以将日期更改为字符串,并在控制器中使用SimpleDataFormat对其进行分析。需要有人接受此答案,因为它只适用于我。非常感谢!
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// change the format according to your need.
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}