Reputation: 155
After submitting the form , I see all the values being passed properly to the controller except date which is passed as null(The date selection in the form enters a proper date in the input field) I tried adding but it did not help. I also tried removing "path" and replacing it with "name" in the "" but it throws error saying "path" attribute is missing. I tried following but no luck-
1. In spring web mvc application date value is received as NULL in controller after reading from date-picker on jsp page
2. http://blog.teamextension.com/date-binding-in-spring-mvc-1321
jQuery:
$(document).ready(function() {
$("#datepicker").datepicker({dateFormat:"yy-mm-dd"});
});
JSP:
<div class="plLabelSearch" id="hiddenField">Due Date:</div>
<div class="plinput"><form:input type="text" id="datepicker" path="dueDate" placeholder="yyyy-mm-dd"/></div>
Model:
@Column(name = "DUE_DATE")
@DateTimeFormat(pattern = "yyyy/mm/dd")
private Date dueDate;
Controller:
@RequestMapping(value="/lock", method = RequestMethod.POST)
public @ResponseBody Status lockDevice(@ModelAttribute("adminTransaction") @Validated AdminTransaction adminTransaction, BindingResult result, Model model, Locale locale,Map<String, Object> map, HttpServletRequest request) {
try {
/*.......CODE.........*/
adminTransactionDO.setDueDate(adminTransaction.getDueDate());
/*.......CODE.........*/
}
return new Status("success", "Transaction inserted Successfully !");
} catch (Exception e) {
return new Status("error", e.toString());
}
}
Upvotes: 2
Views: 13378
Reputation: 566
You could define a transient entity fields for holding the date in String format(Transient are fields that do not participate in persistence and their values are never stored in the database). Then map this field to the path attribute of the input fields of the form in the view. Using the SimpleDateFormat utility within the controller you could convert the date string value into actual date format to be stored into the database.
MODAL
@Column(name = "DUE_DATE")
@DateTimeFormat(pattern = "yyyy/mm/dd")
private Date dueDate;
@Transient
private String dueDateString;
//Your getters, setters and the rest
JSP
<div class="plinput"><form:input type="text" id="datepicker"
path="dueDateString" placeholder="yyyy/mm/dd"/></div>
CONTROLLER
@RequestMapping(value="/lock", method = RequestMethod.POST)
public @ResponseBody Status lockDevice(@ModelAttribute("adminTransaction") @Validated AdminTransaction adminTransaction, BindingResult result, Model model, Locale locale,Map<String, Object> map, HttpServletRequest request) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
try {
Date dueDateVal;
dueDateVal = dateFormat.parse(adminTransaction.getDueDateString());
adminTransaction.setDueDate(dueDateVal);
} catch (ParseException e) {
e.printStackTrace();
}
try {
/*.......CODE.........*/
adminTransactionDO.setDueDate(adminTransaction.getDueDate());
/*.......CODE.........*/
}
return new Status("success", "Transaction inserted Successfully !");
} catch (Exception e) {
return new Status("error", e.toString());
}
}
Upvotes: 0
Reputation: 155
I resolved the issue by passing the date in RequestParam to controller:
public @ResponseBody Status lockDevice(@ModelAttribute("adminTransaction") @Validated AdminTransaction adminTransaction, BindingResult result, Model model, Locale locale,Map<String, Object> map, HttpServletRequest request, @RequestParam @DateTimeFormat(pattern="yyyy-MM-dd") Date dueDate) {
try {
/*.......CODE.........*/
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = dt1.parse(dt1.format(dueDate));
adminTransactionDO.setDueDate(date);
/*.......CODE.........*/
}
return new Status("success", "Transaction inserted Successfully !");
} catch (Exception e) {
return new Status("error", e.toString());
}
}
Upvotes: 1
Reputation: 252
It looks like your :
@Column(name = "DUE_DATE")
@DateTimeFormat(pattern = "yyyy/mm/dd")
private Date dueDate;
Is the problem. Spring is unable to convert it into Date.
Instead try
private String dueDate;
Upvotes: 1
Reputation: 140
You can try this
<div class="plLabelSearch" id="hiddenField">Due Date:</div>
<div class="plinput"><form:input type="date" id="datepicker"/></div>
In your javascript use
var date = document.getElementbyId("datepicker").value;
if you want the date to be sent to servlet do as follows...
<form name="dateform" action="ServletName" method="post">
<input type="date" name="InputDate" id="datepicker"/>
<input type="submit" value="Submit"></form>
in your servlet post method....
String date = request.getParameter("InputDate");
Upvotes: 0