Spring-mvc + Thymeleaf:处理复杂的形式 [英] Spring-mvc + Thymeleaf: dealing with complex form
问题描述
我正在使用spring-mvc和thymeleaf开发内部工具。
I'm working on an internal tool using spring-mvc and thymeleaf.
此工具的一部分用于创建我们保存在数据库中的实体。
这个实体非常复杂;它包含许多属性和关系。其中一些关系包含列表和其他属性。
A section of this tool is used to create an entity we save in the database. This entity is quite complex; it contains many properties and relations. Some of these relations contain list and other properties.
我有2个限制:
- 单页。没有巫师。
- 仅在数据库中保存已完成的对象。
现在,我不是真的要求一个具体的问题。我知道我在百里香的方式,春天@ModelAttribute等等。
Now, I'm not really asking for a specific issue. I know my way around thymeleaf, spring @ModelAttribute, etc.
我的问题主要是你选择哪种策略或如何处理真正复杂的对象创建。
My question is mostly which strategy are you choosing or how to deal with really complex object creation.
现在我可以看到3种方法:
Now I can see 3 ways to do it :
-
渲染页面thymeleaf。每次需要将新元素添加到列表中时,我都使用Ajax在服务器上添加新元素并重新呈现特定片段。因此,使用我的@ModelAttribute来回服务器,并且只在最后保存。
Rendering page with thymeleaf. Every time a new element need to be added to a list, I use Ajax to add the new element on the server and rerender the specific fragment. So doing back and forth to the server with my @ModelAttribute and only save at the end.
使用thymeleaf呈现基本页面。使用JavaScript创建html元素而不是提交给@ModelAttribute,我将我的表单序列化为JSON并将此JSON提交给服务器。 (客户端模型的种类)
Rendering a basic page with thymeleaf. Using JavaScript to create html elements and instead of submitting to a @ModelAttribute, I'm serializing my form to JSON and submit this JSON to the server. (kind of client side model)
使用百里香叶渲染基本页面。当我需要添加列表项时(确保我使用正确的名称=以适合我的Java表单对象),使用JavaScript动态创建html元素,并在最后提交整个内容。
Rendering a basic page with thymeleaf. Create the html element dynamically with JavaScript when I need to add list item (being sure I'm putting proper name="" to fit with my Java form object) and submit the whole thing at the end.
我个人不确定在1或2之间。
我觉得使用JSON比使用表单提交更容易处理复杂对象。此外,具有子对象和属性的输入值/字段可能非常讨厌。拥有这种语法
对我来说听起来不太好...
I'm personally unsure between 1 or 2. I feel dealing with complex object is much more easier using JSON than form submission. Also, the input value/field with sub object and property can be quite nasty. Having this kind of syntax does not sound great to me...
3可能有效但弹簧数据绑定的方式完成在我的拙见中,子属性缺乏一些细节(第7.4.1节 - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html )。
3 can probably work but the way spring data binding is done with sub property is lacking some detail in my humble opinion (section 7.4.1 - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html).
您怎么看?
推荐答案
我个人使用Thymeleaf自己的动态字段管理来确保干净地添加对象和字段对象。
Personally I use Thymeleaf's own dynamic field management to ensure clean addition of objects and fields to object.
所以我建议选择4:Thymeleaf的动态字段管理。
So I will recommend option 4: Dynamic Field management by Thymeleaf.
读一下< a href =http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields =nofollow> http://www.thymeleaf.org/doc/tutorials/2.1/ thymeleafspring.html#dynamic-fields 。
我将它用于单个f现场添加以及添加嵌套表单。诀窍没有问题。
I use that for both single field additions as well as addition of nested forms. Does the trick no questions asked.
希望有所帮助。
这篇关于Spring-mvc + Thymeleaf:处理复杂的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!