【问题标题】:Access variable in jsp tag files inside a loop?访问循环内jsp标记文件中的变量?
【发布时间】:2009-10-10 05:30:31
【问题描述】:

我尝试创建一个 jsp 标记文件,它会调用一个帮助类来打印输入对象。所以我创建了一个文件/WEB-INF/tags/formatter.tag

<%@ 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 获取属性。但我不确定。有人知道吗?

【问题讨论】:

  • “不工作”是什么意思?你得到一个错误或错误的结果?
  • websphere 得到了一些 ArrayIndexOutOfBoundException,由 com.ibm.ws.jsp.translator.visitor.validator.ValidateVisitor.getELExpressions 抛出

标签: jsp file tags


【解决方案1】:

尝试以下方法:

首先,将您的格式化程序转换为标准 Java bean(即,使格式化方法非静态,没有 arg 构造函数等)。的,将标签改为:

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

【讨论】:

  • 就我而言,我需要传递两个参数。例如${formatter.format(value,2)}。这会导致 JSP 错误:无法解析 EL 函数
【解决方案2】:

添加以下内容。您的问题很可能是“c”标签中的任何内容都没有得到处理。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多