嵌套异常是 java.lang.IllegalStateException:无法将类型 [java.lang.String] 的值转换为所需类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10501361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type
提问by dlinx90
I am getting the following validation error when I try to submit my form. The dropdown box populated with values from Document.java gives this error:
当我尝试提交表单时出现以下验证错误。用来自 Document.java 的值填充的下拉框给出了这个错误:
Failed to convert property value of type java.lang.String to required type
testapp.domain.Document for property document_number; nested exception is
java.lang.IllegalStateException: Cannot convert value of type [java.lang.String]
to required type [testapp.domain.Document] for property document_number: no matching
editors or conversion strategy found
Is there something wrong with my mapping?
我的映射有问题吗?
Document.java mapping
Document.java 映射
@OneToMany(mappedBy="document_number", cascade = CascadeType.ALL)
private Set<DocumentRevision> documentRevisions;
public void setDocumentRevisions(Set<DocumentRevision> documentRevisions){
this.documentRevisions = documentRevisions;
}
DocumentRevision.java mapping
DocumentRevision.java 映射
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="DOCUMENT_NUMBER")
private Document document_number;
public void setDocument_number(Document document_number){
this.document_number = document_number;
}
public Document getDocument_number(){
return document_number;
}
The relationship between the two tables is that several DocumentRevisionscan have the same DocumentNumberbut a single DocumentRevisioncan only have one DocumentNumber
两个表的关系是多个DocumentRevisions可以有相同的DocumentNumber,而单个DocumentRevision只能有一个DocumentNumber
Thank you for your help
谢谢您的帮助
/D
/D
Edit
编辑
I am using spring 3.0 and hibernate 3. Hereis a recent thread by me which includes the controller and jsp page.
我正在使用 spring 3.0 和 hibernate 3。这是我最近的一个线程,其中包括控制器和 jsp 页面。
Edit 2
编辑 2
Here is the DAO Implementation method that is supposed to save the document_number into DocumentRevision
这是应该将 document_number 保存到 DocumentRevision 中的 DAO 实现方法
public void saveDocumentRevision(DocumentRevision documentRevision) {
this.sessionFactory.getCurrentSession().save(documentRevision);
}
Edit 3
编辑 3
I believe this is the part of the code where document_number should be recorded. Do I have to use "documentNumberList" somewhere in the .POST method? How would I do that in that case?
我相信这是应该记录 document_number 的代码部分。我是否必须在 .POST 方法中的某处使用“documentNumberList”?在那种情况下我该怎么做?
part of controller:
控制器的一部分:
@RequestMapping(value="/add", method=RequestMethod.GET)
public String getDocumentRevision(Model model) {
DocumentRevision documentRevision = new DocumentRevision();
model.addAttribute("documentRevisionAttribute", documentRevision);
model.addAttribute("documentNumberList", documentService.retrieveAllDocumentNumbers());
return "new-documnent-revision";
}
@RequestMapping(value="/add", method=RequestMethod.POST)
public String postDocumentRevision(@ModelAttribute("documentRevisionAttribute") @Valid DocumentRevision documentRevision, BindingResult result) {
if(result.hasErrors()){
return "new-document-revision";
}
documentRevisionService.createDocumentRevision(documentRevision);
return "redirect:/testapp/document-revision/list";
}
Update
更新
I have added the following to my controller:
我已将以下内容添加到我的控制器中:
/**
* Property editor to map Documents from documents IDs.
*/
class DocumentPropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
Document value = documentService.retrieveDocumentNumber(text);
setValue(value);
}
@Override
public String getAsText() {
Document d = (Document) getValue();
return d != null ? String.valueOf(d.getDocumentNumber()) : "";
}
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Document.class, new DocumentPropertyEditor());
}
I am now getting a NullPointerException:
我现在收到一个 NullPointerException:
java.lang.NullPointerException
testapp.controller.DocumentRevisionController$DocumentPropertyEditor.getAsText(DocumentRevisionController.java:59)
Why isn't the getter in th PropertyEditor getting a value?
为什么 PropertyEditor 中的 getter 没有获得值?
Edit 4
编辑 4
the retrieveDocumentNumber(text) method:
retrieveDocumentNumber(text) 方法:
public Document retrieveDocumentNumber(String text){
return (Document) this.sessionFactory.getCurrentSession().get(Document.class, text);
}
采纳答案by Jose Luis Martin
Spring WebDataBinder fail to convert the String document_number
to a Document instance when
populating the documentRevision model attribute. You have two posibilities:
String document_number
填充 documentRevision 模型属性时,Spring WebDataBinder 无法将 转换为 Document 实例。你有两种可能:
Initialize the
WebDataBinder
with aPropertyEditor
that can handle the conversion. Most direct, but work only for this controller.Register a
Converter<String, Document>
WebDataBinder
用PropertyEditor
可以处理转换的a初始化。最直接,但仅适用于该控制器。注册一个
Converter<String, Document>
if choose the first, annotate a controller method with @InitBinder
annotation and register
the property editor. Seems like you only need to fetch the document from database by document_number. See Customizing WebDataBinder initatizationon reference documentation.
如果选择第一个,用注解注解控制器方法@InitBinder
并注册属性编辑器。似乎您只需要通过 document_number 从数据库中获取文档。请参阅参考文档中的自定义 WebDataBinder 初始化。
Edit
编辑
Property editor example to map Documents to/from Ids Strings.
将文档映射到/从 Id 字符串映射的属性编辑器示例。
/**
* Property editor to map Documents from documents IDs.
*/
class DocumentPropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
Long id = Long.parseLong(text);
Document value = id == 0 ? null : documentService.get(id);
setValue(value);
}
@Override
public String getAsText() {
Document d = (Document) getValue();
return d != null ? String.valueOf(d.getId()) : "";
}
}
For the second approach, look at Validation, Data Binding, and Type Conversionto see
how to create and register a Converter
in the Spring ConversionService
.
对于第二种方法,查看验证、数据绑定和类型转换以了解如何Converter
在 Spring 中创建和注册 a ConversionService
。