Sha*_*que 4 java spring spring-mvc
<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>
Run Code Online (Sandbox Code Playgroud)
下面是我的控制器和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));
}
Run Code Online (Sandbox Code Playgroud)
这样我想要的日期格式不起作用,这是输出我得到的 消费日期= 3月18日05:30:00 IST 2015 但是我希望它变成像yyyy-MM-dd这样的特定格式...建议我这样做的方法.
这不容易吗?
实体或表单备份对象:
class Foo {
/* handles data-binding (parsing) and display if spring form tld or spring:eval */
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private Date expenseDate;
...
}
Run Code Online (Sandbox Code Playgroud)
在一个表格中:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form modelAttribute="modelAttributeName">
<form:input type="date" path="expenseDate" />
</form:form>
Run Code Online (Sandbox Code Playgroud)
或者只是为了显示:
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<spring:eval expression="modelAttributeName.expenseDate" />
Run Code Online (Sandbox Code Playgroud)
一些迂腐的笔记:
请参阅我的帖子以获取最佳实践:Spring MVC:验证,重定向后获取,部分更新,乐观并发,现场安全
我不知道这个答案对您有帮助吗?我的实体类之一,如下所示...
@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;
}
Run Code Online (Sandbox Code Playgroud)
<input type="text" id="incident-date" name="incidentDate" value="23/08/2017" />
Run Code Online (Sandbox Code Playgroud)
弹簧控制器方法在这里..
@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
}
Run Code Online (Sandbox Code Playgroud)
一切正常。在Incident incident包含incidentDate =周三08月23日00:00:00 2017年BDT
如果您不想 @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));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27468 次 |
| 最近记录: |