2009-10-10 174 views
1

我嘗試創建一個jsp標記文件,它將調用幫助器類來打印輸入對象。所以,我創建了一個文件/WEB-INF/tags/formatter.tag在循環內部的jsp標籤文件中訪問變量?

<%@ tag import="package.Formatter"%> 
<%@ attribute name="value" required="true" type="java.lang.Object" %>    
<%=Formatter.format(pageContext.getAttribute("value"))%> 

所以,我可以把它在JSP這樣的:

<t:formatter value="${obj}" /> 

但是我發現,它不會在一個循環內工作,例如

<c:forEach items="${list}" var="i"> 
    <t:formatter value="${i.property}"/> 
</c:forEach> 

我懷疑我不應該從pageContext獲取屬性。但我不確定。任何人都知道這件事?

+0

「不工作」是什麼意思?你得到一個錯誤或錯誤的結果? – moxn 2009-10-10 06:11:48

+0

websphere得到了一些ArrayIndexOutOfBoundException,引發com.ibm.ws.jsp.translator.visitor.validator.ValidateVisitor.getELExpressions – jackysee 2009-10-12 01:45:06

回答

0

嘗試以下操作:

首先,將您的格式是一個標準的Java bean(即使格式方法非靜態,無參數的構造函數等)。將標籤更改爲:

<%@ tag import="package.Formatter" %> 
<%@ attribute name="value" required="true" type="java.lang.Object" %> 
<jsp:useBean id="formatter" class="package.Formatter" />    
${formatter.format(value)} 
+0

仍然是同樣的問題。 – jackysee 2009-10-12 07:04:32

+0

請嘗試新的解決方案 – 2009-10-12 07:51:58

+0

在我的情況下,我需要傳遞兩個參數。例如$ {formatter.format(值,2)}。這會導致JSP錯誤:無法解析EL功能 – jackysee 2009-10-15 09:15:09

0

加入以下內容。很可能你的問題是沒有任何「C」標籤正在處理中。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>