开发者

How to pass Java Date object to dojo DateTextBox through Spring MVC and back to Java?

开发者 https://www.devze.com 2023-01-09 06:18 出处:网络
I wonder how can I pass a Java Date object from beans --> hibernate --> Spring MVC --> dojo and back to that Date object that would be stored in the database using hibernate.

I wonder how can I pass a Java Date object from beans --> hibernate --> Spring MVC --> dojo and back to that Date object that would be stored in the database using hibernate.

I have tried, in beans class, creating getter and setter that return/get String by parsing the value to dojo friendly format (yyyy-MM-dd). When the date from the database is null then there is no problem and everything works as expected, but when the date isn't null the webpage cannot be generated due to the error _841.getMonth() is not a method. I figured out that this is due to spring setting value of form:input ... to a String in format yyyy-MM-dd and dojo not treating it as a Date but as a String.

My code looks like:

<form:input path="tDate" />
<script type="text/javascript">
   Spring.addDecoration(new Spring.ElementDecoration({
      elementId : "tDate",
      widgetType : "dijit.form.DateTextBox",
      widgetAttrs : {promptMessage: "Enter a date",
                     required: "true"}
}));
</script>

and spring converts form:input ... to something like:

<input id="tDate" name="tDate" value="2010-07-29" />

I would greatly appreciate if you could suggest a solution to this problem. If it is possible to have getter and setter for Java Date object to get/return Date and not String, as I am doing now, it would b开发者_运维百科e perfect.

Cheers


Adding:

datePattern : "yyyy-MM-dd"

to widgetAttrs solved the problem. Even though dojo DateTextBox uses this pattern as a default it has to be specified for form:input to work properly.

This solution is not perfect as in beans class I am still using getter and setter to use String for my Date object and parse the value inside these methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消