1.解决上节课程遇到的问题
![添加员工页面](https://img-blog.csdnimg.cn/20190824072013966.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JhaWR1XzE1ODczNTUx,size_16,color_FFFFFF,t_70)
在添加员工信息的时候之后,点击添加没有成功跳转到员工列表页,员工信息也没有添加成功
看标题像是请求了一个get方法,检查代码也没有检查出来问题,debug发现是添加信息之后的后续操作action没有添加,默认也是get方法
<form>
<div class="form-group">
<label>LastName</label>
<input name="lastName" type="text" class="form-control" placeholder="zhangsan">
</div>
<div class="form-group">
<label>Email</label>
修改成.....
<form th:action="@{/emp}" method ="post">
<div class="form-group">
<label>LastName</label>
<input name="lastName" type="text" class="form-control" placeholder="zhangsan">
</div>
2.添加信息后报404错误,查看日志报错“Field error in object 'employee' on field 'birth': rejected value [2019-08-23]; 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 '2019-08-23'; nested exception is java.lang.IllegalArgumentException]]”
提交的数据格式不对:不支持2019-08-23这种格式,只支持2017/12/12这种格式
日期的格式化;SpringMVC将页面提交的值需要转换为指定的类型;
默认日期是按照/的方式;
在配置文件中配置数据格式方式
spring.mvc.date-format=yyyy-MM-dd
查看源码:
WebMvcAutoConfiguration.java
@Bean
@ConditionalOnProperty(
prefix = "spring.mvc",
name = {"date-format"}
)
public Formatter<Date> dateFormatter() {
return new DateFormatter(this.mvcProperties.getDateFormat());
}
WebMvcProperties.java
//默认格式 yyyy/MM/dd
public String getDateFormat() {
return this.dateFormat;
}