在我的Struts2项目中,从我的java操作类返回了一个List<Object>
。对象中的一个数据成员是长格式的日期(来自MySQL数据库)。我无权更改类结构或数据库。我需要将这个日期输出为可读的。我想实现:
<% Date mydate = new Date (long dateAsLong); %>
<%= mydate.toString() %>
这里dateAsLong是返回对象的数据成员。我需要这种帮助,因为我也必须将其应用于其他情况。就像我必须使用检查JSP中的变量一样
<s:if test='<s:property value="priority"/> == 1'>
/*Some img to display here*/
</s:if>
我是个新手,想使用普通的struts2和JSP。请帮我知道如何访问<s:property/>
中返回的变量。
谢谢。
<s:set/>
标记将值放在ValueStack上,这在scriptlet中是不方便的(我建议您无论如何都不要使用scriptlet)。
首先,关于
<s:if test='<s:property value="priority"/> == 1'>
/*Some img to display here*/
</s:if>
试试这个:
<s:if test="%{priority == 1}">
/*Some img to display here*/
</s:if>
您也可以使用JSP EL:
<c:if test="${action.priority == 1}">
/*Some img to display here*/
</c:if>
至于日期,正如doctrey提到的,您可以在操作类中进行转换。我过去曾使用自定义JSP函数处理过这个问题。
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>
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);
}
JSP
<%@ taglib prefix="ex" uri="http://www.example.com/tags/jsp-functions" %>
${ex:longToDate(action.dateAsLong)}