这是一个简单的值bean,使用Spring的新版本(3.0版本) @DateTimeFormat注释(根据我的理解PropertyEditor,根据此SO问题替换了3.0之前的自定义需求):
import java.time.LocalDate;
import org.springframework.format.annotation.DateTimeFormat;
public class Widget {
private String name;
@DateTimeFormat(pattern = "MM/dd/yyyy")
private LocalDate created;
// getters/setters excluded
}
Run Code Online (Sandbox Code Playgroud)
将表单提交中的值与此窗口小部件结合使用时,日期格式可以正常运行.也就是说,只有MM/dd/yyyy格式中的日期字符串才能成功转换为实际LocalDate对象.太棒了,我们就在那里.
然而,我也想能够还显示所创建的LocalDate在同一个JSP视图属性MM/dd/yyyy格式使用JSP EL像这样(假设我的弹簧控制器添加小窗口属性的模型):
${widget.created}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这只会显示默认toString格式LocalDate(yyyy-MM-dd格式).我知道如果我使用spring的表单标签,日期会根据需要显示:
<form:form commandName="widget">
Widget created: <form:input path="created"/>
</form:form>
Run Code Online (Sandbox Code Playgroud)
但我想在不使用spring form标签的情况下显示格式化的日期字符串.甚至是JSTL的fmt:formatDate标签.
来自Struts2,HttpServletRequest包含在一个StrutsRequestWrapper启用这样的EL表达式实际上询问OGNL值栈.所以我想知道spring是否提供类似的东西以允许转换器执行?
编辑
我也意识到,当使用spring的eval标签时,日期将根据@DateTimeFormat注释中定义的模式显示:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:eval expression="widget.created"/>
Run Code Online (Sandbox Code Playgroud)
有趣的是,使用自定义当PropertyEditor格式化日期,该标签不会调用该PropertyEditor …
我已经构建了许多Enum类int getID()和MyEnum withID(int)方法,允许我将ID专用于枚举值以用于持久性目的(从而避免由于枚举的外部存储的顺序/名称更改而导致的更改).
我想构建一个自定义转换器来做一些反射来查找这些方法,并在找不到它们时使用它们或备份到Ordinal/String转换.
一个通用的Enum转换器似乎对任何人都有可能吗?这只是我对转换器的第二次尝试.
我正在尝试使用 Spring MVC、Java 和 MySql 在我的 web 应用程序中实现完整的日历插件。当我尝试在 jsp 中使用“input type = date”添加日期时,出现此错误:
Field error in object 'event' on field 'endDate': rejected value [2018-03-13];
codes [typeMismatch.event.endDate,typeMismatch.endDate,typeMismatch.java.util.Date,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [event.endDate,endDate]; arguments [];
default message [endDate]]; default message [Failed to convert property value of type 'java.lang.String'
to required type 'java.util.Date' for property 'endDate'; nested exception is org.springframework.core.convert.ConversionFailedException:
Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat
java.util.Date for value '2018-03-13';
nested exception is java.lang.IllegalArgumentException: Unable to parse '2018-03-13']
Run Code Online (Sandbox Code Playgroud)
在我的控制器类中,我使用 SimpleDateFormat 来格式化我的日期: …
事实Spring MVC证明,我可以通过多种不同的方式绑定我的表单,但我真的感觉自己迷路了。a 的和方法与具有不同名称(parse和)的 a 的方法等效。同样,我可以实现一个或两个s 来完成完全相同的事情。printFormatterPropertyEditorSupportgetAsTextsetAsTextGenericConverter Converter<S,T>
我在此处Formatters阅读了一条替代 的评论PropertyEditor,但我没有找到任何支持它的文档,而且它甚至还没有被弃用。
我的问题是,当涉及到将数据从表单绑定到对象时,正确的方法是什么spring-mvc?PropertyEditorSpring 中,Formatter和之间的主要区别是什么Converter?每一种的用例是什么?对我来说,他们似乎负有同样的责任。
我的问题是将Spring从表单中获取的数据绑定到JPA实体.奇怪的是,如果我不看BindingResults,它的工作正常.BindingResults说当为字段分度传入一个空字符串时会出现绑定错误,但我知道它确实正确绑定它们,因为当我不检查Hibernate时,它会完美地更新数据库.有没有办法不必编写逻辑来绕过错误触发的绑定错误?
@Entity
@Table(name="child")
public class Child {
@Id
@Column(name="id")
private Integer childId;
@ManyToOne(fetch=FetchType.EAGER )
@JoinColumn(name="house", referencedColumnName="house")
private House house;
@NotNull()
@Past()
@Column(name="birthday")
private Date birthday;
@Column(name="graduation_date")
private Date graduationDay;
}
Run Code Online (Sandbox Code Playgroud)
我在属性编辑器中尝试了以下几行无济于事
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
registry.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
Run Code Online (Sandbox Code Playgroud)
以下是处理请求的控制器方法的方法签名
@Controller
@SessionAttributes(value="child")
@RequestMapping(value="child")
public class ChildModController {
@RequestMapping(value="save-child.do", params="update", method = RequestMethod.POST)
public @ResponseBody Map<String,?> updateChild(
HttpServletRequest request,
@Valid @ModelAttribute(value="child")Child child,
BindingResult results)
}
Run Code Online (Sandbox Code Playgroud)
这是我从BindingResult类中获取的消息
09:01:36.006 [http-thread-pool-28081(5)] INFO simple - Found fieldError: graduationDay,
Failed to convert property …Run Code Online (Sandbox Code Playgroud) java ×5
spring ×4
spring-mvc ×3
el ×1
fullcalendar ×1
hibernate ×1
jpa ×1
jsp ×1
mysql ×1
spring-boot ×1