Take the 2-minute tour ×

In my Struts2 project returned a List<Object> from my java action class. One of the data members in the objects is a date in long form (from MySQL database). I am not authorised to make changes to the class structure or database. I need to output this date as human readable. I want to implement:

<% Date mydate = new Date (long dateAsLong); %>
<%= mydate.toString() %>

Here dateAsLong is data member of the objects returned. I need this help as I have to apply it to other cases as well. Like I have to check the variables in JSP itself using:

<s:if test='<s:property value="priority"/> == 1'>
  /*Some img to display here*/

I am a newbie and want to use plain struts2 and JSP. Please just help me know how I can access that variable returned in <s:property/>.


share|improve this question
Is there anything keeping you from doing so in your action class? You can convert them to date before passing them to the page. –  doctrey Jun 26 '11 at 8:37

1 Answer 1

up vote 2 down vote accepted

The <s:set/> tag places values on to the ValueStack, which isn't conveniently available from scriptlets (I would recommend you avoid scriptlets anyway).

First, on the subject of:

  /*Some img to display here*/

Try this:

<s:if test="%{priority == 1}">
  /*Some img to display here*/

You can also use JSP EL:

<c:if test="${action.priority == 1}">
  /*Some img to display here*/

As for the date, asdoctrey mentioned, you could do this conversion in your action class. I've handled this in the past using a custom JSP function.


  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"

  <description><![CDATA[JSP Functions]]></description>
  <display-name>JSP Functions</display-name>

The Java

 * The JSTL fmt:formatDate tag expects a Date and the fmt:parseDate
 * apparently expects to parse from a String representation of the
 * date. This method allows you to easily convert a long into a Date
 * object for use in fmt:formatDate.
 * <p/>
 * The long passed in to this method is expected to be a UNIX epoch
 * timestamp (number of milliseconds since Jan. 1, 1970). For more
 * details, see {@link java.util.Date#Date(long)}.
 * @param epoch The long to convert. Expected to be an epoch timestamp.
 * @return A new Date object.
public static Date longToDate(final long epoch) {
  return new Date(epoch);


<%@ taglib prefix="ex" uri="http://www.example.com/tags/jsp-functions" %>

share|improve this answer

Your Answer


