简体   繁体   中英

Spring MVC updating an object via form

I am currently sending a logged in User object to view which will be injected into a form via th:object. I want to update certain attributes in this User object but still maintain the rest of the object's content. However, when I submit this form, the User object contains null values for all values except the one I have set in the thymeleaf page. I know one solution would be to add hidden tags for the values I want to keep but that seems very tedious if the User object is huge.

@RequestMapping(value="/newprofile", method=RequestMethod.GET)
public String newProfile(Model model, Principal principal) {
    String email = principal.getName();
    User user = userService.findUserByEmail(email);
    model.addAttribute("user", user);
    return "newprofile";
}

@RequestMapping(value="/newprofile", method=RequestMethod.POST)
public String registerNewProfile(Model model,User user, Principal principal) {
    userService.saveProfile(user); //this user object will contain null values
    return "redirect:/profile";
}

Here is how the form looks. The user object that comes in is an existing User with its values already set. There are member variables that can be updated.

<form autocomplete="off" action="#" th:action="@{/newprofile}" th:object="${user}" method="post" class="form-signin" role="form">
    <h3 class="form-signin-heading">Registration Form</h3>
    <div class="form-group">
        <div class="">
            <input type="text" th:field="*{profile.basicInfo.age}" placeholder="Name" class="form-control" />
        </div>
    </div>
    <div class="form-group">
        <div class="">
            <button type="submit" class="btn btn-primary btn-block">Update profile</button>
        </div>
    </div>
</form>

Once the form is submitted, I perform a save of that User object via Spring JPA's save() method. However, if the User object contains nulls, it will incorrectly "update" those values to null. Again, I can do some checking to validate which members should be updated and which should not but that seems incorrect...

@Override
public User saveProfile(User user) {
    // TODO Auto-generated method stub
    userRepository.save(user);
    return user;
}

We can use the bean validation API annotations on model class attributes like below:

@NotNull(message = "Name cannot not be null")
private String name;

Similar use of @NotEmpty , @NotBlank

By this, it won't enter if any of the validation fails.

If copying the existing User bean before update is an option, following api may be used.

BeanUtils. copyProperties

Please do note that there are two popular BeanUtils.copyProperties. One from Apache and the other one mentioned with this post. The order of method parameters for both these api are different.

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