1

我有下面的代码:

<c:set var="show" value="#{cartBean.itemsAdded}" />

<c:if test="${show}">
    <h:form id="test1">
        <h:commandLink action="#{cartBean.foo}">this doesn't work</h:commandLink>           
    </h:form>
</c:if>

<h:form id="test2">
    <h:commandLink action="#{cartBean.foo}">this works!</h:commandLink>         
</h:form>

当 show=false 时,只显示第二个链接。它有效。我可以到达服务器(我正在使用调试来查看这个)。

当 show=true 时,两个链接都会出现。但只有第二个链接有效。条件内的链接不会触发服务器中的操作。

有人,可以请帮助我吗?

注意:当我使用a4j:outputPanel rendering="#{show}"时会发生同样的事情

4

3 回答 3

2

在处理表单提交期间,JSF 将重新评估是否呈现了命令按钮/链接。如果它没有被渲染,那么它会简单地跳过这个动作。

当 JSF 处理表单提交时,您需要确保表达式也#{cartBean.itemsAdded}返回。true一个简单的测试是将 bean 放在会话范围内(我假设isItemsAdded()是一个纯 getter,即它只包含return itemsAdded;)。

如果这确实解决了问题并且您希望将 bean 保留在请求范围内,那么添加 a<a4j:keepAlive>以在后续请求中保留 bean 属性。

<a4j:keepAlive beanName="#{cartBean}" />

也可以看看:


与具体问题无关,您应该尽可能喜欢 JSF 标记/属性而不是 JSTL 标记/属性。在这种特殊情况下,您应该去掉这两个 JSTL<c:>标记并改用 JSF 提供的rendered属性:

<h:form id="test1" rendered="#{cartBean.itemsAdded}">
    <h:commandLink action="#{cartBean.foo}">this doesn't work</h:commandLink>           
</h:form>
于 2011-03-17T16:17:21.450 回答
1

解决方法

我不想使用 sessionScope,因为在一个巨大的系统中使用它有危险(我的例子)。我不喜欢使用keepAlive neighter,因为我在一个杂乱的服务器中并且许多属性是不可序列化的。

无论如何,我找到了这个解决方法:

  1. 在请求中发送参数(如 show=true)
  2. 更改检查方法,在返回中添加OR以查看此新参数。

管理豆:

前:

public boolean itemsAdded() {
    return foo; // my initial check
}

后:

public HttpServletRequest getRequest() {
        return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
}

public boolean itemsAdded() {
    return foo || getRequest().getParameter("show") != null;
}

XHTML:

前:

<c:set var="show" value="#{cartBean.itemsAdded}" />
<c:if test="${show}">
    <h:form id="test1">
        <h:commandLink action="#{cartBean.foo}">link</h:commandLink>           
    </h:form>
</c:if>

后:

<c:set var="show" value="#{cartBean.itemsAdded}" />
<c:if test="${show}">
    <h:form id="test1">
       <h:commandLink action="#{cartBean.foo}">link
          <f:param name="show" value="true"/>
       </h:commandLink> 
    </h:form>
</c:if>
于 2011-04-15T17:30:35.960 回答
0

改进的(和微小的)解决方法:

仅更改 XHTML:

前:

<c:if test="#{cartBean.itemsAdded}">
    <h:form id="test1">
        <h:commandLink action="#{cartBean.foo}">link</h:commandLink>           
    </h:form>
</c:if>

后:

<c:if test="#{cartBean.itemsAdded || params['show']}">
    <h:form id="test1">
       <h:commandLink action="#{cartBean.foo}">link
          <f:param name="show" value="true"/>
       </h:commandLink> 
    </h:form>
</c:if>
于 2011-04-15T17:53:38.290 回答