Form binding to private fields

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Form binding to private fields

greenfly556
I'm using Spring Boot 2.1.6 and Thymeleaf. Is there a way to allow form binding to private fields on objects? My form can display the value if there isn't a public getter, but it won't post the value back in the @ModelAttribute if there is no public setter.

For example:
<form method="post" role="form" th:action="@{/company/create}" th:object="${company}">
  <div>
    <label>Company name*</label>
      <input class="form-control" name="name" placeholder="Enter company name" th:field="*{companyName}"  type="text"/>
  </div>

  <div class="box-footer">
      <button class="btn btn-primary" type="submit">Submit</button>
  </div>
</form>
I only get the company name back if I create Company.setCompanyName(...). It can show the company name even without Company.getCompanyName(...). I'd like to not have to create a separate object for form binding, but nor do I want a setter for company name on my entity - the value should not change after creation.