gpt4 book ai didi

java - jSTL中如何获取值

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:12 24 4
gpt4 key购买 nike

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

我可以在 c:set 的值属性中调用一个方法吗?如果不能,请告诉我该怎么做。

异常(exception):

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>

最佳答案

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

在您的 bean 中使用临时属性:

<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>

作为参数,您可以使用 roleManager 本身和参数。

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

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

关于java - jSTL中如何获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6830316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com