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

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

问题描述

我正在使用Spring MVC的SimpleFormController结合Spring MVC的JTL格式创建一个表单来编辑Generic对象。



在我的表单上我有一个下拉列表用户可以通过下拉列表指定服务器。

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

这里的服务器通过数据库调用传播到所有可用的服务器。服务器是一个服务器ORM pojo,它是作为我的表单支持对象的另一个ORM pojo(Generic)的子对象。



我的目标是改变Generic的服务器引用,在数据库级别表示为服务器表的外键。



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



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

 字段'server'上的对象'generic'中的字段错误:reject value [1];代码[typeMismatch.generic.server,typeMismatch.server,typeMismatch.com.generic.orm.jpa.Server,typeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:codes [generic.server,server];参数[];默认消息[服务器]];默认消息[属性server将[java.lang.String]类型的属性值转换为必需类型[com.generic.orm.jpa.Server];嵌套异常是java.lang.IllegalArgumentException:不能将类型[java.lang.String]的值转换为属性server所需的类型[com.generic.orm.jpa.Server]:找不到匹配的编辑器或转换策略], generic = com.generic.orm.jpa.generic [id = 3]}和静态属性{} 

我一直在寻找一个如何完成这个没有运气的例子。我相信我需要覆盖SimpleFormController中的内容,就像我在这个问题,但是Spring MVC的文档很详细。任何人都可以帮助我吗?

解决方案

我认为你是正确的。您需要像之前所做的那样在活页夹上注册一个自定义编辑器,以便将String转换为服务器实例。有一个自定义PropertyEditor的例子这里在Spring参考文档中应该让你开始。



我同意MVC文档不是最好的。我不得不在Spring论坛上做很多Google搜索和搜索。


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>

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.

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.

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

Unfortunately this doesn't seem to be binding properly when my form submits, as it can't translate from String to Server.

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 {}

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?

解决方案

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.

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.

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

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