如何使用<;s:属性值=“0”;var"/&燃气轮机;Struts2中jsp上scriplets中的变量?

如何使用<;s:属性值=“0”;var"/&燃气轮机;Struts2中jsp上scriplets中的变量?,jsp,jakarta-ee,struts2,Jsp,Jakarta Ee,Struts2,在我的Struts2中,项目从java操作类返回了一个列表。对象中的一个数据成员是长格式的日期(来自MySQL数据库)。我无权更改班级结构或数据库。我需要把这个日期输出为可读的。 我想实施: <% Date mydate = new Date (long dateAsLong); %> <%= mydate.toString() %> 此处dateAsLong是返回对象的数据成员。 我需要这个帮助,因为我必须把它应用到其他情况。我必须使用以下方法检查JSP本身中的变

在我的Struts2中,项目从java操作类返回了一个
列表。对象中的一个数据成员是长格式的日期(来自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>

/*这里显示一些img*/
我是一个新手,希望使用纯struts2和JSP。请告诉我如何访问
中返回的变量

谢谢。

标记将值放在ValueStack上,这在Scriptlet中不方便使用(我建议您无论如何都不要使用Scriptlet)

首先,关于以下主题:

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

/*这里显示一些img*/
试试这个:

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

/*这里显示一些img*/
您还可以使用JSP EL:

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

/*这里显示一些img*/
至于日期,asdoctrey提到,您可以在action类中进行此转换。我以前使用自定义JSP函数处理过这个问题

TLD

JSP函数
1
前任
http://www.example.com/tags/jsp-functions
爪哇
/**
*JSTL fmt:formatDate标记需要一个日期,fmt:parseDate标记需要一个日期
*显然希望从
*日期。此方法允许您轻松地将长日期转换为日期
*在fmt中使用的对象:formatDate。
*

*传递给此方法的时间很长,预计将是一个UNIX时代 *时间戳(自1970年1月1日起的毫秒数)。更多 *详细信息,请参见{@link java.util.Date#Date(long)}。 * *@param epoch要转换的时间很长。应该是一个历元时间戳。 *@返回一个新的日期对象。 */ 公共静态日期(最后一个长纪元){ 返回新日期(历元); }

JSP

${ex:longtoate(action.dateAsLong)}

在动作课上,有什么东西阻止你这么做吗?在将它们传递到页面之前,可以将它们转换为日期。
<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 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" %>

${ex:longToDate(action.dateAsLong)}