Hibernate 由于bindingresult中的错误,返回页面后在jsp中格式化日期视图
我有一个愚蠢的问题。我想在用户输入后更改表单中显示的日期格式。 我遇到了这样的情况: 用户使用日期选择器插入两个日期 然后,如果bindingResult中出现错误,则返回相同的页面,但日期如下所示: 我只想在第二个图像中显示日期,就像第一个图像中的日期一样。谢谢 编辑 这是包含日期的模型Hibernate 由于bindingresult中的错误,返回页面后在jsp中格式化日期视图,hibernate,date,jsp,spring-mvc,date-formatting,Hibernate,Date,Jsp,Spring Mvc,Date Formatting,我有一个愚蠢的问题。我想在用户输入后更改表单中显示的日期格式。 我遇到了这样的情况: 用户使用日期选择器插入两个日期 然后,如果bindingResult中出现错误,则返回相同的页面,但日期如下所示: 我只想在第二个图像中显示日期,就像第一个图像中的日期一样。谢谢 编辑 这是包含日期的模型 @Entity public class Booking implements Serializable { // private Date startDate; private Date finish
@Entity
public class Booking implements Serializable {
//
private Date startDate;
private Date finishDate;
//
它们通过Hibernate存储在表中
在控制器中,我必须进行验证转换,因为从mysql返回的日期(我想是这样)。我从db得到的格式是“2017年7月1日星期六00:00:00 CEST”,而我从datepicker得到的格式是“yyyy-mm-dd HH:mm:ss”
jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(function () {
$("#startDate,#finishDate").datepicker();
});
<form:form action="${pageContext.request.contextPath}/rent" method="POST" modelAttribute="newBooking">
<div>
<table class="table">
<input id="id" name="id" type="hidden" value="${vehicle.id}"/>
<tr>
<td><form:label path="startDate">Data Inizio</form:label></td>
<td><form:input type="text" id="startDate" name="startDate" path="startDate" data-format="dd/MM/yyyy" /> </td>
<td><form:errors path="startDate" cssStyle="color: #ff0000" /></td>
</tr>
<tr>
<td><form:label path="finishDate">Data Fine</form:label></td>
<td><form:input type="text" id="finishDate" name="finishDate" path="finishDate" /></td>
<td><form:errors path="finishDate" cssStyle="color: #ff0000" /></td>
</tr>
</table>
<input type="submit" value="Invia" />
<br>
<br>
<a href="${pageContext.request.contextPath}/">Torna alla Home</a>
</div>
</form:form>
$(函数(){
$(“#开始日期,#完成日期”).datepicker();
});
数据日化
数据精
您可以尝试将此批注添加到您的bear@DateTimeFormat中
@Entity
public class Booking implements Serializable {
@DateTimeFormat(pattern = "MM/dd/yyyy")
private Date startDate;
@DateTimeFormat(pattern = "MM/dd/yyyy")
private Date finishDate;
您需要在JSP中使用的日期属性中添加一个格式化程序,您使用的是spring吗?是的,我使用spring,我在JSP中使用了它:但是这样,在返回页面后数据就不会显示了。您可以将bean发送到JSP的问题添加到您的问题中吗?我添加了一些信息;)它起作用了!但是,如果a将使用“dd/MM/yyyy”这样的日期格式?我尝试使用@DateTimeFormat(pattern=“dd/MM/yyyy”),但结果是“MM/dd/yyyy”应该适用于MM/dd/yyyy检查您没有覆盖jspi中的格式值必须更改验证并在日期选择器中应用格式,现在可以了;)谢谢@麻烦大了!很好的工作,还可以检查响应是否有效,以便对其他用户有用?谢谢
@Entity
public class Booking implements Serializable {
@DateTimeFormat(pattern = "MM/dd/yyyy")
private Date startDate;
@DateTimeFormat(pattern = "MM/dd/yyyy")
private Date finishDate;