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 Dat开发者_如何学Goe (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*/
</s:if>
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/>
.
Thanks.
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:
<s:if test='<s:property value="priority"/> == 1'>
/*Some img to display here*/
</s:if>
Try this:
<s:if test="%{priority == 1}">
/*Some img to display here*/
</s:if>
You can also use JSP EL:
<c:if test="${action.priority == 1}">
/*Some img to display here*/
</c:if>
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.
The TLD
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description><![CDATA[JSP Functions]]></description>
<display-name>JSP Functions</display-name>
<tlib-version>1.0</tlib-version>
<short-name>ex</short-name>
<uri>http://www.example.com/tags/jsp-functions</uri>
</taglib>
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);
}
The JSP
<%@ taglib prefix="ex" uri="http://www.example.com/tags/jsp-functions" %>
${ex:longToDate(action.dateAsLong)}
精彩评论