将字符串转换为 Spring MVC 表单数据绑定的自定义对象? [英] Converting from String to custom Object for Spring MVC form Data binding?

查看:39
本文介绍了将字符串转换为 Spring MVC 表单数据绑定的自定义对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Spring MVC 的 SimpleFormController 与 Spring MVC 的表单 JTL 结合使用来创建一个表单来编辑通用对象.

I am using Spring MVC's SimpleFormController in conjunction with Spring MVC's form JTL to create a form to edit a Generic object.

在我的表单上,我有一个下拉菜单,用户可以在其中通过下拉菜单指定服务器.

On my form I have a drop down where the user can specify a server via a drop down.

<form:form commandName="generic">
    <form:select path="server">
        <form:options items="${servers}" itemValue="id" itemLabel="name"/>
    </form:select>
</form:form>

此处的服务器通过对所有可用服务器的数据库调用进行传播.server 是一个 Server ORM pojo,它是另一个 ORM pojo(通用)的子对象,用作我的表单支持对象.

Servers here is propagated by a database call for all available servers. server is a Server ORM pojo, that is a sub-object of another ORM pojo (Generic) that serves as my form backing object.

我在这里的目标是更改 Generic 的服务器引用,它在数据库级别表示为服务器表的外键.

My goal here is to change Generic's server reference, which is represented on the database level as a Foreign Key to the server table.

我使用 JPA 作为我的持久层,使用 JPA 生成的实体类作为我的 ORM pojo.

I am using JPA as my persistence layer and JPA generated entity classes as my ORM pojos.

不幸的是,当我提交表单时,这似乎没有正确绑定,因为它无法从字符串转换为服务器.

Field error in object 'generic' on field 'server': rejected value [1]; codes [typeMismatch.generic.server,typeMismatch.server,typeMismatch.com.generic.orm.jpa.Server,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [generic.server,server]; arguments []; default message [server]]; default message [Failed to convert property value of type [java.lang.String] to required type [com.generic.orm.jpa.Server] for property 'server'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.generic.orm.jpa.Server] for property 'server': no matching editors or conversion strategy found], generic=com.generic.orm.jpa.generic[id=3]} and static attributes {}

我一直在寻找如何在没有运气的情况下完成此任务的示例.我相信我需要覆盖 SimpleFormController 中的某些内容,就像我在 this question,但 Spring MVC 的文档在细节上很简单.有人可以帮我吗?

I've been looking for an example of how to accomplish this with no luck. I believe I need to overwrite something within the SimpleFormController, like I did in this question, but Spring MVC's documentation is light on details. Can anyone help me out here?

推荐答案

我认为你是对的.您需要像以前一样在活页夹上注册一个自定义编辑器,以便将 String 转换为 Server 实例.有一个自定义 PropertyEditor 的例子 此处 应该让您开始使用 Spring 参考文档.

I think you are correct. You need to register a custom editor on the binder like you done before so you can convert a String to a Server instance. There is an example of a custom PropertyEditor here in the Spring reference docs that should get you started.

我同意 MVC 文档不是最好的.我不得不在 Spring 论坛上进行大量谷歌搜索和搜索.

I agree that the MVC documentation is not the best. I have had to do a lot of Googling and searching on the Spring forums.

这篇关于将字符串转换为 Spring MVC 表单数据绑定的自定义对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆