5

I have a JSP page where i am getting a Date value from my action class. I am not able to understand how it is handled as:

<s:property value="#someDate"/> 

gives me date

2/7/14

whereas

<s:property value="{#someDate}"/> 

gives me Date as

[Wed Feb 7 00:00:00 IST 2014]

Can someone tell me how date value is actually handled here, as date is returned in different formats?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Sonal Maheshwari
  • 217
  • 2
  • 3
  • 12
  • The property of `someDate` is which type .. ? I mean String or Date type – Rookie007 Feb 07 '14 at 07:36
  • I dont know much .. I think struts2 `` tag evaluates the variable in valuestack by the notion that you have used to get the variable when you use `#` key is used to access the variables has been set by the `` tag when you use that `{#someDate}` it will get the value as type of the variable that has been set by its setter. And when you use `#someDate` it normally searches the value stack by the name `someDate` and returns as `String` that format `2/7/14` is the real format that pushed to the `someDate` variable.. for more info check ognl documentation. – Rookie007 Feb 07 '14 at 08:03
  • If I am wrong some one correct me . plz – Rookie007 Feb 07 '14 at 08:04
  • Why are you using the property tag and not the tag? This tag is especially for date output handling. – Johannes Feb 07 '14 at 12:00
  • ya, We can use tag.. but i just want to know the reason of difference of results in the cases mentioned. – Sonal Maheshwari Feb 10 '14 at 05:50
  • http://stackoverflow.com/questions/21607045/how-come-struts-property-tag-converts-java-util-date-in-m-d-yy-format – Andrea Ligios Feb 10 '14 at 12:38
  • Thanks for the link... that helped me to understand :) – Sonal Maheshwari Feb 11 '14 at 15:07

1 Answers1

9

Nice Question.

<s:property value="{#someDate}"/> is equal to <s:property value="someDate.toString()"/> or ${someDate} where as <s:property value="someDate"/> is using built in type conversion of xwork2 which uses the SHORT format for the Locale associated with the current request for dates.

See Built in Type Conversion Support

value="{#someDate}" means value="someDate.toString()"

it converts date to date.tosting() that is why you are getting [Wed Feb 7 00:00:00 IST 2014]

To handle date formats there is a special tag in struts2

<s:date name="someDate" format="dd/MM/yyyy" />

Prints

17/04/2014

Also see

<s:date name="someDate" format="dd/MMM/yyyy" />

Prints

17/Apr/2014 

Also there is attibute nice="true"

<s:date name="someDate" nice="true" />

Prints

 2 days ago
prem30488
  • 2,828
  • 2
  • 25
  • 57