简体   繁体   中英

thymeleaf :: populate the form with data from the service

I have a controller as below

@RequestMapping(value = "/user/{userId}/personal", method = RequestMethod.GET)
public String getUserPersonalInfo(@PathVariable("userId") Integer userId, Model model) throws ServiceBusinessException {

    UserBO userBO = userServiceImpl.findUserByUserId(userId);
    model.addAttribute("user", userBO);
    model.addAttribute("userId", userId);
    model.addAttribute("genders", EnumSet.allOf(Gender.class));
    model.addAttribute("maritalStatuses", EnumSet.allOf(MaritalStatus.class));
    model.addAttribute("ethnicities", EnumSet.allOf(Ethnicity.class));
    System.out.println(EnumSet.allOf(Ethnicity.class));
    return "personal";
}

Please note the following :

this is a GET call made from the UI to the controller, the controller then processes the request and returns the user along with the other information to the view (personal.html).

Now on the personal.html i need to have a way to edit this user information. thus I am thinking of binding this user data in a th:form tag

this form would then be used to send a post request to the controller to save the changes made by the user.

The problem is that I have not seen any code examples where this has been done. All examples which I have seen, the form is empty and the page is then loaded. user fills up the empty form and click on submit, this information is then stored in the db.

I however on the other hand, is doing something opposite. I need to populate the form with the user object on page load and then on submit this form should be saved in the database.

Can someone guide me to this example please ?

@user641887

While populating the user details on page load, bind data using th:field for a read-only form and when user clicks on edit button make the form fields which are allowed to edit as editable. Now user can edit the required information and save the details.

Sample thymeleaf form code

    <input type="text" th:field="*{datePlanted}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM