java SpringBoot/SpringSecurity登录用户无法更新其详细信息,未收到错误,但数据库中未发生更新

jjhzyzn0  于 2023-02-07  发布在  Java
关注(0)|答案(1)|浏览(132)

我试图允许我的Spring应用程序的当前登录用户更新他们当前的详细信息,但它没有持续到数据库,我没有得到任何错误,并试图调试,但没有得到成功..请看一看。
服务等级:

@Transactional
public User updateAccount(User userInForm){
    System.out.println("Fetching user with id: " + userInForm.getId());

    Optional<User> optionalUser = repo.findById(userInForm.getId());
    if(!optionalUser.isPresent()){
        System.out.println("User not found.");
        return null;
    }

    User userInDB = optionalUser.get();
    System.out.println("User fetched: " + userInDB);

    userInDB.setFirstName(userInForm.getFirstName());
    userInDB.setLastName(userInForm.getLastName());
    System.out.println("Saving updated user: " + userInDB);

    User savedUser = repo.save(userInDB);
    System.out.println("User saved: " + savedUser);

    return savedUser;
}

控制器类:

@PostMapping("/myAccount/update")
public String updateAccount(User user, RedirectAttributes redirectAttributes,    Principal principal){
    System.out.println("Updating user details...");
     user = repo.findByEmail(principal.getName());
    User updatedUser = service.updateAccount(user);

    if (updatedUser == null) {
        System.out.println("Error updating user details.");
    } else {
        redirectAttributes.addFlashAttribute("message", "Details Updated!");
        return "redirect:/myAccount";
    }

    return "redirect:/myAccount";
}

前端:

<h1 style="color:green">Welcome <b>[[${#request.userPrincipal.principal.fullName}]]</b></h1>
<h2 style="color:green">My Details</h2>
<div th:if="${message}" class ="alert alert-success text-center">
    [[${message}]]
</div>
<form th:action="@{/myAccount/update}" th:object="${user}"
      method="post" style="max-width: 600px; margin: 0 auto;">

    <div class="m-3">
        <div class="form-group row">
            <label class="col-4 col-form-label">E-mail: </label>
            <div class="col-8">
                <input type="email" th:field="*{email}" class="form-control" readonly="readonly" />
            </div>
        </div>

        <div class="form-group row">
            <label class="col-4 col-form-label">Password: </label>
            <div class="col-8">
                <input type="password" th:field="*{password}"  placeholder="Leave blank if you don't want to change!" class="form-control"
                        minlength="6" maxlength="10"/>
            </div>
        </div>

        <div class="form-group row">
            <label class="col-4 col-form-label">First Name: </label>
            <div class="col-8">
                <input type="text" th:field="*{firstName}" class="form-control"
                       required minlength="2" maxlength="20"/>
            </div>
        </div>

        <div class="form-group row">
            <label class="col-4 col-form-label">Last Name: </label>
            <div class="col-8">
                <input type="text" th:field="*{lastName}" class="form-control"
                       required minlength="2" maxlength="20" />
            </div>
        </div>

        <div>
            <button type="submit" class="btn btn-primary">Update Details</button>
        </div>
    </div>
</form>

控制台中的打印语句:

Updating user details...
Fetching user with id: 1
User fetched: com.example.Model.User@330603d0
Saving updated user: com.example.Model.User@330603d0
User saved: com.example.Model.User@330603d0
ddarikpa

ddarikpa1#

您正在将从请求中接收的用户对象重新分配为其他值。请检查控制器方法中的以下行

user = repo.findByEmail(principal.getName()); // this line reassigning the user object from the request to that of the one in database.
User updatedUser = service.updateAccount(user);

因此,用户详细信息将更新,但使用现有数据。

相关问题