Java springmvc中yyyy-MM-dd格式的日期绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29183871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Date binding in springmvc in yyyy-MM-dd format
提问by Sharique
<tr class="rowsAdded">
<td><input name="item" class="form-control" type="text" placeholder="Item" /></td>
<td><input name="amount" class="form-control" type="number" placeholder="Amount" /></td>
<td><input name="expenseDate" class="form-control" type="date"placeholder="ExpenseDate" /></td>
</tr>
Below is my controller and Init Binder
下面是我的控制器和 Init Binder
@RequestMapping (value = "/saveExpenses", method=RequestMethod.POST)
public String saveExpenses (@RequestBody ExpenseDetailsListVO expenseDetailsListVO, Model model,BindingResult result) {
if (result.hasErrors()) {
System.out.println(result.getFieldError().getField().toString()+" error");
}
System.out.println(expenseDetailsListVO);
return "success";
}
@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
In this way the date format which I want is not working, this is the output what I am getting expenseDate=Wed Mar 18 05:30:00 IST 2015But I want it into a particular format like yyyy-MM-dd... suggest me the way to do it.
这样,我想要的日期格式不起作用,这是我得到的输出 费用日期=Wed Mar 18 05:30:00 IST 2015但我希望它变成像 yyyy-MM-dd 这样的特定格式...建议我这样做的方法。
回答by fsaftoiu
What I am saying is that your binder probably works just fine. What it does is it takes a String in format yyyy-MM-dd from your HTTP request's body and converts it into a Date object. But that's all it does. What you do with that Date object after that is up to you. If you want to pass it along as a String to some other part of your program you should convert it, perhaps using SimpleDateFormatter.
我的意思是你的活页夹可能工作得很好。它的作用是从 HTTP 请求的正文中获取格式为 yyyy-MM-dd 的字符串,并将其转换为 Date 对象。但这就是它所做的。之后您如何处理该 Date 对象取决于您。如果您想将它作为字符串传递给程序的其他部分,您应该转换它,也许使用 SimpleDateFormatter。
回答by Neil McGuigan
Wouldn't this be easier?
这不是更容易吗?
Entity or Form-Backing-Object:
实体或表单支持对象:
class Foo {
/* handles data-binding (parsing) and display if spring form tld or spring:eval */
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private Date expenseDate;
...
}
In a form:
以一种形式:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form modelAttribute="modelAttributeName">
<form:input type="date" path="expenseDate" />
</form:form>
Or just to display:
或者只是为了显示:
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<spring:eval expression="modelAttributeName.expenseDate" />
Some pedantic notes:
一些迂腐的笔记:
- Use CSS for layouts, not tables. See Bootstrap's grid system.
- Use the Post-Redirect-Getpattern for forms.
- Use Spring's Form taglib for proper HTML escaping and CSRF protection
- Use @Validated in your controller handler methods to validate
- You're missing a space before "placeholder" in your form
- 将 CSS 用于布局,而不是表格。请参阅 Bootstrap 的网格系统。
- 对表单使用Post-Redirect-Get模式。
- 使用 Spring 的 Form taglib 进行正确的 HTML 转义和 CSRF 保护
- 在控制器处理程序方法中使用 @Validated 进行验证
- 您在表单中的“占位符”之前缺少一个空格
See my post here for best practices: Spring MVC: Validation, Post-Redirect-Get, Partial Updates, Optimistic Concurrency, Field Security
有关最佳实践,请参阅我的帖子:Spring MVC: Validation、Post-Redirect-Get、Partial Updates、Optimistic Concurrency、Field Security
回答by Mr. Mak
I don't know will this answer help you or not ? One of my Entity Class like below...
我不知道这个答案对你有帮助吗?我的实体类之一,如下所示...
@Entity
@Table(name = "TAIMS_INC_INCIDENT")
public class Incident implements Serializable{
@DateTimeFormat(pattern = "dd/MM/yyyy") // This is for bind Date with @ModelAttribute
@Temporal(TemporalType.DATE)
@Column(name = "inc_date")
private Date incidentDate;
}
<input type="text" id="incident-date" name="incidentDate" value="23/08/2017" />
Spring Controller Method Here..
弹簧控制器方法在这里..
@RequestMapping(value = "/saveIncident.ibbl", method = RequestMethod.POST)
public ModelAndView saveIncident(
@ModelAttribute("incident")
Incident incident,
BindingResult result){
System.out.println(incident.getIncidentDate());
// Wed Aug 23 00:00:00 BDT 2017
}
This is working fine. The Incident incident
Contains incidentDate = Wed Aug 23 00:00:00 BDT 2017
这工作正常。在Incident incident
包含incidentDate =周三08月23日00:00:00 2017年BDT
If you do not want to use@DateTimeFormat(pattern = "dd/MM/yyyy")
in Entity class then put the method below in Controller Class...
如果您不想@DateTimeFormat(pattern = "dd/MM/yyyy")
在实体类中使用,则将下面的方法放在控制器类中...
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}