简体   繁体   English

更新用户数据弹簧启动的最佳方法是什么?

[英]What is the best way to update user data spring boot?

I am not able to succeed in doing correct way to update a user data like his firstname or lastname 我无法以正确的方式成功更新用户数据,例如他的名字或姓氏

Controller: 控制器:

@Controller
public class UpdateUserData {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/user/updateFirstName", method = RequestMethod.POST)
    public String changeUserFirstName(RedirectAttributes rm, final Locale locale, @Valid UserDto userDto) {
        final User user = (User) SecurityContextHolder.getContext()
            .getAuthentication()
            .getPrincipal();
        userService.changeUserFirstName(user, userDto.getFirstName());

        rm.addFlashAttribute(
            "message",
            messages.getMessage("message.updateFirstNameSuc", null, locale)
        );
        return "redirect:/user/myAccount?lang=" + locale.getLanguage();
    }
}

And that is the part related in myAccount html page: 这就是myAccount html页面中相关的部分:

<div class="manageinfo">
   <h1>Manage your personal information</h1>
   <br/>
   <div class="container">
      <div th:if="${message != null}" class="alert alert-info" 
         th:text="${message}">message</div>
      <div th:each = "user: ${user}" >
         <p>Firstname</p>
         <input type="text" th:value="${user.firstName}"><a data-toggle="modal" 
            data-target="#myModalFirstName"><i class="fa fa-pencil fa-lg"> </i></a>
      </div>
      <!-- Modal for firstName-->
      <div class="modal fade" id="myModalFirstName" role="dialog">
         <button type="button" class="close" data-dismiss="modal">&times;</button>
         <form th:action="@{/user/updateFirstName}" th:method="POST">
            <p>First Name</p>
            <input type="text" name="firstName" required="required">
            <span id="firstNameError"  style="color:#F41F0E" ></span>     
            <button type="submit" class="btn btn-default" >Save</button>
         </form>
      </div>
   </div>
</div>

UserServiceImpl: UserServiceImpl:

public void changeUserFirstName(final User user, final String firstname) {
    user.setFirstName(firstname);
    userRepository.save(user);   
}

When I try to change the name I will receive 当我尝试更改名称时,我会收到

Failed to resolve argument 2 of type Myproject.dto.UserDto
javax.validation.ValidationException: HV000028: Unexpected exception during isValid call

The logical update procedure seems to be ok, I would suggest to check the UserDto and it validation annotation (@NotNull, @Size, etc..) over it fields, as isValid is thrown by @Valid annotation of your'e controller as data did not follow the given constraints. 逻辑更新过程似乎还可以,我建议检查UserDto及其在其字段上的验证注释(@ NotNull,@ Size等。),因为isValid由控制器控制器的@Valid注释作为数据抛出没有遵循给定的约束。

Eg, Your UserDTO might contain: 例如,您的UserDTO可能包含:

@Size(max = 30)
private String firstName;
@Pattern(regexp = "....")
private String lastName;

But your'e controller have recieved a firstName with size larger than maximum / firstName with a wrong pattern. 但是您的控制器收到的FirstName的大小大于maximum / firstName的大小,且带有错误的模式。

Read also: https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html#isValid-T-javax.validation.ConstraintValidatorContext- 阅读: https : //docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html#isV​​alid-T-javax.validation.ConstraintValidatorContext-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB 和 Spring 引导 - 更新文档的最佳方式? - MongoDB and Spring Boot - Best way to update a Document? 在 spring 引导中更新实体的最佳方法 - The best way to update entity in spring boot 在 Spring Boot 中处理异常的最佳方法是什么? - What is the best way to handle exception in Spring Boot? 在spring boot jpa中存储日期的最佳方法是什么? - What's the best way to store date in spring boot jpa? 在 Spring 引导中返回每个 API 的统一响应的最佳方法是什么? - What is the best way to return the uniform response for each API in Spring Boot? 用Thymeleaf在Spring Boot中记录活动的最佳方法是什么? - What is the best way to log Activity in Spring Boot with Thymeleaf? 在 Spring Boot 应用程序中安排任务的最佳方法是什么 - What is best way to schedule task in spring boot application 在Spring Boot中对时间表任务进行软编码的最佳方法是什么? - What is the best way for softcoding schedule tasks in Spring Boot? 检查 Spring 引导 RestAPI 的字段的最佳方法是什么 - What is the best way to check which fields are given to the Spring boot RestAPI 在 Spring Boot 应用程序中处理 json 文件的最佳方法是什么? - What is the best way to process a json file in spring boot app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM