【问题标题】:how to get the value in jstl如何获取jstl中的值
【发布时间】:2011-07-26 12:53:50
【问题描述】:

我在 jstl 中写了一段代码。执行 html 时出现以下错误。

我可以在 c:set 的 value 属性中调用一个方法吗?如果不能,请帮助我如何做到这一点。

例外:

com.sun.facelets.tag.TagAttributeException: /role/MyPage.xhtml @33,82 value="#{roleManager.roleStatus(roleId)}" Error Parsing: #{roleManager.roleStatus(roleId)}

代码:

<select name="123">
        <c:forEach items="#{roleManager.addRoleList}" var="category">
        <c:set var="roleId" value="#{category.value}" />
        <c:set var="roleIdValue" value="#{roleManager.getRoleStatus(roleId)}" />
            <c:if test="${roleIdValue}">
                <option value="#{roleId}" style="color:#990000;"> <h:outputLabel  value="#{category.key}" /></option>
            </c:if>
            <option value="123"> <h:outputLabel  value="#{category.key}"/></option>
        </c:forEach>
    </select> 

【问题讨论】:

    标签: java jsf jstl


    【解决方案1】:

    标准 el 解析器无法评估带参数的方法调用。 以下是一些解决方案:

    在 bean 中使用 temp 属性:

    <c:set target="${roleManager}" property="roleId" value="${roleId}"/>
    <c:set var="roleIdValue" value="#{roleManager.roleStatus}" />
    

    您还需要将以下代码添加到您的 bean:

    private String roleId;
    
    public String getRoleStatus() {
        // Invocation of your logic with the parameter.
        return getRoleStatus(getRoleId());
    }
    
    public String getRoleId() {
        return roleId;
    }
    
    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }
    

    使用函数:

    在页面上:

    ${prefix:methodName(param1, param2, ...)}
    

    你应该在 taglib 中声明函数:

    <function>
    <name>methodName</name>
    <function-class>className</function-class>
    <function-signature>
        returnType methodName(param1Type, param2Type, ...)
    </function-signature>
    

    作为参数,您可以使用您的角色管理器本身和参数。

    使用允许方法调用的 el-resolver:

    例如使用 JBoss el 解析器, 或者您也可以按照此处所述实施您自己的解决方案: http://technology.amis.nl/blog/622/how-to-call-methods-from-el-expressions-pre-jsp-20-trick-for-jsps-with-jstl

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多