| Author |
JSTL: formatDate Vs Timestamp
|
Celso Oliveira
Greenhorn
Joined: Sep 06, 2006
Posts: 3
|
|
Hi everyone. It�s my first post here. I�m having some trouble and I don�t know what to do. I have a JavaBean, User, with the following attribute: private Timestamp birthDate; I�m trying to use the <c:set> tag to set this attribute when I load a JSP page. here�s what I�m doing: And here�s the error I get: Do I have to build my own custom-tag to deal with java.sql.Timestamp ??
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
I've never tried, but I'm wondering if you could do the following: 1. Create a PropertyEditor for Timestamp. Use PropertyEditorSupport and override setAsText(String text). Make sure that the format of the text parameter is the same as the one you've got with fmt:parseDate 2. At the server startup (use a listener), register your PropertyEditor using the PropertyEditorManager.registerEditor(Class targetType, Class editorClass) 3. Try again This sounds fun to do I may try.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56229
|
|
<c:set target="${objUsr}" property="birthDate" value="<fmt:parseDate value='${param.bDate}' type='date' dateStyle='short' pattern='dd/MM/yyyy'/>"/>
You cannot use an action as the attribute value of another action.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
Here it is. This has been fun. I'll leave you tweak the date format. Maybe you should avoid using Timestamp after all  The PropertyEditor The ContextListener The user bean Listener registration in web.xml The JSP test file
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56229
|
|
Ummmmmm, well, I guess Rube Goldberg would be proud? You could just move the <fmt> action to the body of the <c:set>: [ September 06, 2006: Message edited by: Bear Bibeault ]
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
I guess Rube Goldberg would be proud?
I actually wanted to try the PropertyEditor, which I've never used. Spring is using the same kind of stuff to register custom property editors to set beans' properties. (But your one liner broke my heart ;) )
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56229
|
|
Didn't mean to break your heart , but why over-complicate matters when a simple answer will suffice? [ September 06, 2006: Message edited by: Bear Bibeault ]
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
By the way, are you sure this works with Timestamp ? I've tried with but got an exception : cannot convert java.util.Date into java.sql.Timestamp.
|
 |
Stefan Evans
Bartender
Joined: Jul 06, 2005
Posts: 1005
|
|
No, JSTL can't convert a java.util.Date into a java.sql.Timestamp automatically. There are a couple of approaches I can think of: 1 - Add a setter for the property that takes a java.util.Date, and converts it to a timestamp internally. ie 2 - in the bean use java.util.Date rather than java.sql.Timestamp to declare the property. Convert it to a java.sql.Timestamp (if necessary) in the JDBC code. That separates the Bean from your data access layer. Hope this helps, evnafets
|
 |
Celso Oliveira
Greenhorn
Joined: Sep 06, 2006
Posts: 3
|
|
Hi people. Thanks everyone for the sugestions. It�s strange... When I try to show(get) a Timestamp attribute with JSTL it�s OK. No erros. When I try to set....All my problems start.... I�ll try somethings.... Thank you all again!!!
|
 |
Stefan Evans
Bartender
Joined: Jul 06, 2005
Posts: 1005
|
|
Its because java.sql.Timestamp extends java.util.Date So you can use a Timestamp object anywhere you use a Date (thus your display code works) However anywhere that specifes timestamp (such as your save code) will not accept just plain Date objects - only Timestamps.
|
 |
Celso Oliveira
Greenhorn
Joined: Sep 06, 2006
Posts: 3
|
|
Thanks Satou Your sugestion worked FINE!!! I had to make a few adjustments but it was OK!!!  Thank you very much!!!
|
 |
 |
|
|
subject: JSTL: formatDate Vs Timestamp
|
|
|