Spring MVC - 将空日期作为参数传递

时间:2016-04-28 16:19:18

标签: java spring 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

我可以解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

只需将其添加到您的控制器即可。

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