解决JSTL的formatNumber ,formatDate 不能接受表达式的错误

JSTL1.2.jar

今天在使用<fmt:formatNumber>标记时遇到一个错误,我的Jsp页面写法如下:

<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>,标记使用写法如下:

<fmt:formatNumber type="number" 
       value="${(fList.addr-(fList.addr%10))/10}">
</fmt:formatNumber>

结果在运行时报如下错误:

According to TLD or attribute directive in tag file, attribute value does not accept any expressions。

还算可以看得懂,大致意思是value属性不能接受表达式。在网上找资料也是稀里糊涂,只好跑到jar包中看了一下相应的tld文件,

原来uri="http://java.sun.com/jstl/fmt"的写法在fmt-1_0.tld中描述,<tlib-version>1.0</tlib-version>,选用的是jstl1.0版,

其中的formatNumber标记中有如下一段描述:

<tag-class>org.apache.taglibs.standard.tag.el.fmt.FormatNumberTag</tag-class>

且其中的所有属性描述都包含如下段:

<rtexprvalue>false</rtexprvalue>

说明是不能使用EL表达式的,恍然大悟。

然而fmt-1_0-rt.tld虽然和fmt-1_0所声明的tlib-version和jsp-version都一样,但其使用的tag-class不一样:

<tag-class>org.apache.taglibs.standard.tag.rt.fmt.FormatNumberTag</tag-class>

在该formatNumber标记的属性描述中都包含如下段:

<rtexprvalue>true</rtexprvalue>

说明是可以使用EL表达式的,^_^。

在Jsp页面中uri的写法为uri="http://java.sun.com/jstl/fmt_rt"。

另外在jstl1.2.jar中还有一个fmt.tld,其中<tlib-version>1.1</tlib-version>,说明选用jstl1.1版本。

在Jsp页面中uri的写法为uri=http://java.sun.com/jsp/jstl/fmt,formatNumber的value属性支持EL表达式


转载出处:http://blog.csdn.net/debbykindom/archive/2010/09/01/5854964.aspx

原文地址:https://www.cnblogs.com/thewindkee/p/12873187.html