如何在 Spring Web Flow 2 中编写自定义转换器?
我将 Web Flow 2.0.7 与 Spring MVC 和 Hibernate 一起使用。
我的问题是关于我的自定义类型的自定义转换器以及转换器内的数据库连接。
假设我有一个类型 Person,并且 Person 有一个自定义类型 Title 的字段,以及所有 Titles已经在我的数据库中了。 现在我有一个 html 表单,用户可以在其中填充 Person 实例,包括在选择下拉框中选择 Title。
在流程定义中,我从数据库中获取所有标题,并使用自定义转换器将它们显示在下拉框中,将标题转换为字符串并稍后回到标题。
我的问题是关于从 String (这是数据库 ID,我将其设置为元素上的值)转换回数据库中正确的 Title 对象的过程。 基本上:如何做?
到目前为止,我无法将 titleManager 注入到我的转换器中以访问数据库。 Spring Web Flow 论坛对此场景进行了评论。 另一种解决方案可能是在渲染视图之前缓存标题,并在表单发布后以某种方式获取内存中的标题。
如果有人能启发我如何处理这种数据绑定,我将非常感激。 到目前为止我还无法让它工作,因此,我从其他很棒的网络流中得到的使用很少。
我已经在 Web Flow Board 上发布了一个帖子,但仍然缺少一个最佳帖子-练习,我自己无法找到。
太感谢了!
钨
I am using Web Flow 2.0.7 with Spring MVC and Hibernate.
My problem is about custom converters for my custom types and database connection from within my converter.
Let's say I have a type Person and the Person has a field of my custom type Title, and all Titles are already in my database. Now I have an html form, in which a user can populate a Person instance, including selecting the Title in a select drop-down box.
In the flow definition I get all Titles from the database and they are shown in the dropdown box using a custom converter, converting Title to String and later back to Title.
My question is about the process of converting back from String (which is the database ID, which I set as value on the element) to the correct Title object from my database. Basically: How to do it?
So far, I was unable to get a titleManager injected into my converter to get access to the database. This scenario was commented on in the Spring Web Flow Forum. Another solution might be to cache the Titles before rendering the view and somehow get the in-memory Title after the form was POSTed.
I would really appreciate it, if someone could enlighten me, how to handle this kind of data binding. I was unable to get it working so far and thus, I get minimal use out of the otherwise awesome webflows.
I already posted a thread on the Web Flow Board, but still missing a best-pratice, which I am unable to find by myself.
Thank you so much!
Wolfram
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我经常这样做。
基本上我加载标题列表并将其放入我的表单模型中。 在表单模型中,我还有一个 currentTitleId 或 selectedTitleId 变量来存储所选项目的值。 该字段名称在 spring 组合框的“path”中设置,titleList 在“items”中设置。 然后,在“itemValue”中设置要绑定的值,并在“itemLabel”中设置该值的显示文本。 就是这样。
在我的表单模型中:
在我的 jsp 中:
我假设您的 Title 类将是这样的:
您还可以自定义组合框,如下所示:
I use to do this.
Basically I load the list of Titles and put it in my form model. In the form model I have also a currentTitleId or selectedTitleId variable to store the value of the selected item. This field name is set in the "path" of the spring combobox and the titleList is set in the "items". Then the value that you want to bind is set in the "itemValue" and the text to be shown for that value in "itemLabel". That's it.
In my form model:
In my jsp:
I assume that your Title class will be something like this:
You can also customize you combobox a little more like this:
我不太确定 Spring Web Flow,但是使用普通的 Spring MVC 注册一个新的 PropertyEditor 就足够了,然后这个东西就会自动工作
http://static.springframework.org/spring/docs/2.5.x/reference/validation.html #beans-beans-conversion-customeditor-registration
http://static.springframework.org/spring/docs/2.5.x/reference/mvc.html#mvc-ann-webdatabinder
所以我会创建一个新的 PropertyEditor 来获取服务或 dao 负责从数据库获取数据,在 PropertyEditor 中,您可以将 id 转换为您的键类型,并从数据库获取值并将其返回。 我只是没有合适的例子,但我希望你能明白要点。
I'm not so sure about Spring Web Flow, but with the normal Spring MVC it is sufficient to register a new PropertyEditor and then this stuff works automatically
http://static.springframework.org/spring/docs/2.5.x/reference/validation.html#beans-beans-conversion-customeditor-registration
http://static.springframework.org/spring/docs/2.5.x/reference/mvc.html#mvc-ann-webdatabinder
So I would create a new PropertyEditor which gets a service or dao which would be responsible for getting the data from the database, and within the PropertyEditor you would convert the id to your key type and get the value from the database and return it. I just don't have an example right with me, but I hope you get the gist.