This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

SpringMVC form handling, domain objects fields not present form are nullified after commit

Ranch Hand
Posts: 111
PHP Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just starting with Spring/Hibernate integration and have some problems in handling the form commit. My application to develop is concentrated around the domain objects Activity and Member.
The application core functionality consists of members who can create events (like outdoor events, movies, dinners) where other members can subscribe for.

The domain objects are defined as follows with Hibernate annotations (getter and setters omitted):

I am starting with the functionality of adding and editing activities for the logged in member (which is now hardcoded because the login system still has to be developed).

I am using a Spring annotated controller and Thymeleaf view. THe problem occurs when editing an existing activity.

This is the code for the controller:

The form part of the view file looks like this:

Most tutorials and examples I find on the internet have domain objects without id field and always have input form fields for all of the available domain object fields.

First I noticed that the id field itself was set to null in the submitForm() method when using the "/formsubmit" pattern. I changed it to "/{id}/formsubmit" and extract the id using a PathVAriable annotation. An alternative would be using a hidden id attribute as one sees a lot in legacy web applications.

I am not sure what is the best practice here. But in this way it is working anyway.

But still the "organiser" field is nullified after the submit. THe organiser of an event cannot be modified after the first create and therefore is not in the form. It is probably possible to use a hidden field for this property too but I thought this should not be necessary with Spring MVC and form backing objects.
So I think my question boils down to how to retains the values of domain object fields which are references to other domain objects and -in rthis case- not editable after the first time create and not part of the edit form (and therefore not part of the POST request variables).
Any tips are more than welcome!
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!