Search code examples
javajspstruts2struts-tags

How struts property tag works with date value?


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?


Solution

  • 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