2016-04-28 54 views
0

我试图在我的控制器上实现一个过滤器。Spring MVC - 将空日期作为参数传递

这是控制器

@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"; 
    } 

我把PARAMS成图,然后,在DAO的水平,这PARAMS将创建限制查询。我在将日期时间字段留空时出现问题

我从简单的表单中查询字符串。如果我提供了正确的日期,那么每个其他提交的表单都可以正常工作。当我把它留空时,我得到:

org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.util.Date] for value ''; nested exception is java.lang.IllegalArgumentException 

我可以解决这个问题吗?

+0

您是否尝试跳过此参数? – mariusz2108

+0

如果我跳过它,打算删除参数,一切正常......如果我在其中输入正确的日期,它也可以工作。 – besmart

+0

我不是说删除控制器。只是不通过它。你设置required = false。如果你通过''作为日期,你得到一个错误,因为''不能转换为日期对象 – mariusz2108

回答

0

只需将此添加到您的控制器,它应该工作。

@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)); 
}