SpringBoot框架之增删改查员工时注意点

修改员工时:

  1. 由于save()方法中先判断id==null时会自增,故回显表单时,若不携带员工id来到回显编辑页面,则修改后会默认id自增新添加一个员工。
    解决方法:
    在回显编辑的表单中添加id隐藏域
<input type="hidden" name="id" th:value="${empById.getId()}">
  1. 由于服务器在创建员工出生日期未做格式化修改,浏览器默认格式为2021/2/15 11:10:00这种格式,故回显到表单时也应做格式修改
    否则报错如下:

There was an unexpected error (type=Bad Request, status=400).
Validation failed for object=‘employee’. Error count: 1
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object ‘employee’ on field ‘birth’: rejected value [2021/02-15 11:06:50]; codes [typeMismatch.employee.birth,typeMismatch.birth,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.birth,birth]; arguments []; default message [birth]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Date’ for property ‘birth’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value ‘2021/02-15 11:06:50’; nested exception is java.lang.IllegalArgumentException]

解决方法:使用${#dates.format(原始日期,转化格式)

<input th:value="${#dates.format(empById.getBirth(),'yyyy/MM/dd HH:mm:ss')}" name="birth" type="text">

猜你喜欢

转载自blog.csdn.net/m0_47119598/article/details/113814446