除了“不要那样做”之外,我想你可以测试updatedDate的类型:
<c:choose>
<c:when test="${review.updatedDate.class.name == 'java.util.Date'}">
Date: ${review.updatedDate}
</c:when>
<c:otherwise>
Map: ${review.updatedDate._value}
</c:otherwise>
</c:choose>
...假设日期是Date类的一个实例。奇怪的是,当我尝试测试 java.util.HashMap 时,这种方法不起作用。
也许更可靠的方法是将测试交给 Java 类:
package typetest;
import java.util.Map;
public class TypeUtil {
public static boolean isMap(Object o) {
return o instanceof Map;
}
}
这可以通过 TLD 文件(例如 WEB-INF/maptest.tld)映射到自定义函数:
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>myfn</short-name>
<uri>http://typeutil</uri>
<function>
<name>isMap</name>
<function-class>typetest.TypeUtil</function-class>
<function-signature>boolean isMap(java.lang.Object)</function-signature>
</function>
</taglib>
导入函数的示例 JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="myfn" uri="http://typeutil"%>
<html>
<body>
<c:choose>
<c:when test="${myfn:isMap(review.updatedDate)}">
Map: ${review.updatedDate._value}
</c:when>
<c:otherwise>
Date: ${review.updatedDate}
</c:otherwise>
</c:choose>
</body>
</html>