2016-04-28 16 views
0

私のコントローラの1つでフィルタを実現しようとしています。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は、クエリに制限が作成されますです。 dateTimeフィールドを空白にしておくと問題が発生します。

私は単純なフォームからクエリ文字列を取ります。フォームに記入されたものはすべて動作し、正しい日付を入力すると動作します。私はそれを空白のままにすると、私は得る:

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

私はそれをスキップし、paramの削除を意図して、すべての作品...私はそれに正しい日付を入れても動作します。 – 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)); 
} 
関連する問題