最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Spring-mvc + Thymeleaf: dealing with complex form - Stack Overflow

programmeradmin2浏览0评论

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 plex; it contains many properties and relations. Some of these relations contain list and other properties.

I have 2 constraints:

  1. Single page. No "wizard".
  2. To only save a pleted object in the database.

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 plex object creation.

Now I can see 3 ways to do it :

  1. 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.

  2. 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)

  3. 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.

I'm personally unsure between 1 or 2. I feel dealing with plex 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 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 - .html).

What do you think ?

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 plex; it contains many properties and relations. Some of these relations contain list and other properties.

I have 2 constraints:

  1. Single page. No "wizard".
  2. To only save a pleted object in the database.

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 plex object creation.

Now I can see 3 ways to do it :

  1. 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.

  2. 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)

  3. 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.

I'm personally unsure between 1 or 2. I feel dealing with plex 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 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).

What do you think ?

Share Improve this question asked Jan 4, 2015 at 17:34 rguillemetterguillemette 531 silver badge6 bronze badges 1
  • Did the answer help? – Aeseir Commented Nov 16, 2015 at 3:17
Add a ment  | 

1 Answer 1

Reset to default 5

Personally I use Thymeleaf's own dynamic field management to ensure clean addition of objects and fields to object.

So I will remend option 4: Dynamic Field management by Thymeleaf.

Have a read of http://www.thymeleaf/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields.

I use that for both single field additions as well as addition of nested forms. Does the trick no questions asked.

Hope that helps.

发布评论

评论列表(0)

  1. 暂无评论