Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 652
Sonal Maheshwari Avatar asked Feb 07 '14 07:02

Sonal Maheshwari


People also ask

What is a date tag?

The date tag allows to format a Date in a quick and easy way. User can specify a custom format (eg. "dd/MM/yyyy hh:mm"), can generate easy readable notations (like "in 2 hours, 14 minutes"), or can just fall back on a predefined format with key 'struts. date.

What are Struts tags?

You can create Struts HTML tags from the Struts HTML Tags drawer. These tags are used to create Struts input forms, as well as other tags generally useful in the creation of HTML-based user interfaces. The output is HTML 4.01 compliant or XHTML 1.0 when in XHTML mode.


1 Answers

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
like image 149
prem30488 Avatar answered Oct 05 '22 22:10

prem30488