我将在Roo中为日期字段构建一个查找程序,但在运行应用程序并使用查找程序时,我会获得BindExceptions。
实施例
我改变了诊所的例子
为宠物添加野外生日
field date --fieldName birthDay --type java.util.Date --notNull --dateTimeFormatPattern "dd.MM.yyyy"
为宠物添加实体投影
entity projection --class ~.domain.PetInfo --entity ~.domain.Pet --fields id,name,type,birthDay,owner --entityFormatExpression "#{name} (#{type}) - #{owner.lastName} #{owner.lastName} "
repository jpa --entity ~.domain.Pet --interface ~.repository.PetRepository --defaultReturnType ~.domain.PetInfo
删除宠物的发现者
添加formbean并找到宠物生日
dto --class ~.domain.PetBirthdayFormBean
field date --fieldName birthDay --type java.util.Date --dateTimeFormatPattern "dd.MM.yyyy"
find add --entity ~.domain.Pet --name findByBirthDay --formBean ~.domain.PetBirthdayFormBean --returnType ~.domain.PetInfo
并添加我用formbean删除的一个查找器
dto --class ~.domain.PetTypeAndNameLikeFormBean
field string --fieldName name
field enum --fieldName type --type ~.domain.reference.PetType
finder add --entity ~.domain.Pet --name findByTypeAndNameLike --formBean ~.domain.PetTypeAndNameLikeFormBean --returnType ~.domain.PetInfo
当我开始申请时,我得到例外
2017-05-11 09:30:45.618 WARN 9476 --- [nio-8092-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved exception caused by Handler execution: org.springframework.validation.BindException: org.sp
ringframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'formBean' on field 'birthDay': rejected value [Thu May 11 00:00:00 CEST 2006]
; codes [typeMismatch.formBean.birthDay,typeMismatch.birthDay,typeMismatch.java.util.Date,typeMismat
ch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [formBean.
birthDay,birthDay]; arguments []; default message [birthDay]]; default message [Failed to convert pr
operty value of type [java.lang.String] to required type [java.util.Date] for property 'birthDay'; n
ested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert fro
m type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.D
ate] for value 'Thu May 11 00:00:00 CEST 2006'; nested exception is java.lang.IllegalArgumentExcepti
on: Invalid format: "Thu May 11 00:00:00 CEST 2006"]
我使用另一个查找器查看列表生日而不是日期是一系列数字。列出所有宠物的生日显示为日期。
查看图片
因为您认为这不是答案,但我找到了一个问题的解决方案并回答了问题
为什么我不能回答我自己的问题???? `
现在我发布的问题答案不干净我认为
要查找的Datefield不起作用我还没解决。
但是查找视图中的Datefields和其他Objectfield(不是字符串和简单类型)的问题我找到了问题和解决方案。 与https://jira.spring.io/browse/ROO-3907相同的问题。
正常的控制器示例PetsCollectionThymeleafController方法数据表:
@GetMapping(produces = Datatables.MEDIA_TYPE, name = "datatables", value = "/dt")
@ResponseBody
public ResponseEntity<ConvertedDatatablesData<Pet>> datatables(DatatablesColumns datatablesColumns, GlobalSearch search,
DatatablesPageable pageable, @RequestParam("draw") Integer draw) {
Page<Pet> pets = getPetService().findAll(search, pageable);
long totalPetsCount = pets.getTotalElements();
if (search != null && StringUtils.isNotBlank(search.getText())) {
totalPetsCount = getPetService().count();
}
ConvertedDatatablesData<Pet> datatablesData = new ConvertedDatatablesData<Pet>(pets, totalPetsCount, draw,
getConversionService(), datatablesColumns);
return ResponseEntity.ok(datatablesData);
}
searchController示例PetsSearchThymeleafController方法byNameAndWeightDt:
@GetMapping(name = "byNameAndWeightDt", value = "/byNameAndWeight/dt", produces = Datatables.MEDIA_TYPE)
@ResponseBody
public ResponseEntity<DatatablesData<PetInfo>> byNameAndWeightDt(@ModelAttribute("formBean") PetNameAndWeightFormBean
formBean, GlobalSearch search, DatatablesPageable pageable, @RequestParam(Datatables.PARAMETER_DRAW) Integer draw) {
Page<PetInfo> pets = getPetService().findByNameAndWeight(formBean,search,pageable);
long totalPetsCount = pets.getTotalElements();
if (search != null && StringUtils.isNotBlank(search.getText())) {
totalPetsCount = getPetService().countByNameAndWeight(formBean);
}
DatatablesData<PetInfo> datatablesData = new DatatablesData<PetInfo>(pets, totalPetsCount, draw);
return ResponseEntity.ok(datatablesData);
}
在Method datatables中,返回Type为responseEntity&gt;并将使用ConvertedDatatablesData设置datatablesData = new ConvertedDatatablesData(pets,totalPetsCount,draw,
getConversionService(),datatablesColumns);
PetsCollectionThymeleafController类具有attribut conversionService和getter以及setter方法。在construktor中,将设置此字段。
private ConversionService conversionService;
...
@Autowired
public PetsCollectionThymeleafController(PetService petService, ConversionService conversionService, MessageSource
messageSource, ControllerMethodLinkBuilderFactory linkBuilder) {
setPetService(petService);
setConversionService(conversionService);
setMessageSource(messageSource);
setItemLink(linkBuilder.of(PetsItemThymeleafController.class));
}
...
public ConversionService getConversionService() {
return conversionService;
}
public void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}
...
ConversionService转换表格中的元素。
但SearchControllern中的Methods不使用ConversionService,因此像Owner这样的Object将在List中显示为[object Object],Dates将显示为1500501600000。
将ConversionService添加到SearchContoller并在Constructor中设置。 通过..dt更改returntype方法并设置新参数DatatablesPageable pageable。在FactoryClass中,by..dt将在调用中调用set new null参数。 设置返回元素
ConvertedDatatablesData<Pet> datatablesData = new ConvertedDatatablesData<Pet>(pets, totalPetsCount, draw,
getConversionService(), datatablesColumns);
更改PetsSearchThymeleafController:
...
private ConversionService conversionService;
...
@Autowired
public PetsSearchThymeleafController(PetService petService, ConversionService conversionService, MessageSource
messageSource) {
setPetService(petService);
setMessageSource(messageSource);
setConversionService(conversionService);
}
...
public ConversionService getConversionService() {
return conversionService;
}
public void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}
...
@GetMapping(name = "byNameAndWeightDt", value = "/byNameAndWeight/dt", produces = Datatables.MEDIA_TYPE)
@ResponseBody
public ResponseEntity<ConvertedDatatablesData<PetInfo>> byNameAndWeightDt(DatatablesColumns datatablesColumns,
@ModelAttribute("formBean") PetNameAndWeightFormBean formBean, GlobalSearch search, DatatablesPageable
pageable, @RequestParam(Datatables.PARAMETER_DRAW) Integer draw) {
Page<PetInfo> pets = getPetService().findByNameAndWeight(formBean,search,pageable);
long totalPetsCount = pets.getTotalElements();
if (search != null && StringUtils.isNotBlank(search.getText())) {
totalPetsCount = getPetService().countByNameAndWeight(formBean);
}
ConvertedDatatablesData<PetInfo> datatablesData = new ConvertedDatatablesData<PetInfo>(pets, totalPetsCount, draw,
getConversionService(), datatablesColumns);
return ResponseEntity.ok(datatablesData);
}
...
更改前
改变之后
答案 0 :(得分:1)
你的问题让我记得以下一个:
https://jira.spring.io/browse/ROO-3887
我们已经解决了RC1版本的问题。但是,在分析了您的问题之后,您似乎遇到了与此问题出现在finder结果表中的已注册问题相同的问题:
https://jira.spring.io/browse/ROO-3898
目前,还没有自动解决方案,但您可以手动解决!编辑包含查找结果表的list.html
,并添加额外的{
以将格式应用于日期字段。
请参阅:
data-th-field="${{birthDay}}"
希望它有所帮助,