I am developing web application using Spring 3.2.4. I have some forms with fields containing date and time. Piece of my jsp:
<form:form method="post" action="/add" modelAttribute="licence">
...
<form:input type="datetime" path="beginDate"/>
<form:input type="datetime" path="endDate"/>
<form:input path="quantityLimit"/>
...
</form:form>
Normal form, nothing fancy. I am using datepicker, which gives me date in format yyyy-MM-dd HH:mm
, so I've added this to my controller:
@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
dateFormat.setLenient(true);
webDataBinder.registerCustomEditor(DateTime.class, new CustomDateEditor(dateFormat, true));
}
Also, I have added <mvc:annotation-driven/>
to my servlet configuration xml, as stated on some blogs.
There's target controller:
@RequestMapping(value = "/{softwareId}/licence/add", method = RequestMethod.POST)
public String addLicence(@PathVariable("softwareId") Long softwareId, Licence licence, Model model) {
Software software = softwareRepository.findOne(softwareId);
licence.setSoftware(software);
licenceRepository.save(licence);
return ADMIN_PATH + "softwareEdit";
}
And software class looks like this:
@Entity
@Table(name = "licences")
public class Licence {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "begin_date")
private DateTime beginDate;
@Column(name = "end_date")
private DateTime endDate;
@Column(name = "quantity_limit")
private Long quantityLimit;
@ManyToOne
private Software software;
//getters, setters, etc.
}
The problem is: when I submit my form with dateTime field empty it works perfectly, but when I have anything in date field (no matter if it's properly formatted or not) I get HTTP Error 400: Bad Request
. No exceptions in console, only bad request, but I am pretty sure it has something to do with date parsing.
Is there a well described method of dealing with date and time fields in forms in Spring applications?
getTime(); DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); String formattedDate=dateFormat. format(date); System. out. println("Current time of the day using Calendar - 24 hour format: "+ formattedDate); } . . .
We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.
Make your life simple and use @DateTimeFormat
, getting rid of your WebDataBinder
configuration. It seems CustomDateEditor
only works with java.util.Date
and Spring has no other (default/not-specified) mechanism to convert from a String
to a DateTime
.
@DateTimeFormat
is such a mechanism.
@Column(name = "begin_date")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private DateTime beginDate;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With