gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:59:35 24 4
gpt4 key购买 nike

我正在尝试在我的一个 Controller 上实现过滤器。

这是 Controller

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

我将参数放入映射中,然后在 DAO 级别,此参数将创建查询限制。我在将日期时间字段留空时遇到问题

我从一个简单的表单中获取查询字符串。表格中提交的所有其他内容都有效,如果我输入正确的日期,它也有效。当我将其留空时,我得到:

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

我可以解决这个问题吗?

最佳答案

只需将其添加到您的 Controller 中,它就应该可以工作。

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

关于java - Spring MVC - 将空日期作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36920259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com