简体   繁体   English

JSF <h:inputText> / <h:outputText> 不根据布尔值呈现

[英]JSF <h:inputText> / <h:outputText> not rendering according to boolean value

I have a <h:dataTable> that is populated from an Entity class and displays different products. 我有一个<h:dataTable> ,它是从Entity类填充的,并显示不同的产品。 I want to be able to click a <h:commandLink> and edit row that the link belongs to. 我希望能够单击<h:commandLink>并编辑链接所属的行。 I have structured it mostly the same way as this nice example and article at mkyong . 我的结构基本上与mkyong上的这个漂亮示例和文章相同

The table (with some columns excluded): 该表(不包括某些列):

<h:form>
    <h:dataTable id="gnomeTable" value="#{gnome.productList}" 
                 var="item" 
                 styleClass="gnomeTable"
                 headerClass="gnomeTableHeader"
                 rowClasses="gnomeTableOddRow,gnomeTableEvenRow"
                 bgcolor="#F1F1F1" border="10"  cellspacing="2"
                 rules="all" >           

        <f:facet name="header">
            <h:outputText value="List of available gnomes for sale" />
        </f:facet> 

        <h:column>
            <f:facet name="header">
                <h:outputText value="GnomeID" />
            </f:facet> 
            <h:outputText value="#{item.id}"></h:outputText>
        </h:column>

        <h:column>
            <f:facet name="header">
                <h:outputText value="Name"/>
            </f:facet> 
            <h:outputText value="#{item.name}" rendered="#{not item.editable}"></h:outputText>
            <h:inputText value="#{item.name}" rendered="#{item.editable}"></h:inputText>
        </h:column>

        <h:column>
            <f:facet name="header">
                <h:outputText value="Available"/>
            </f:facet>
            <h:outputText value="#{item.available}" rendered="#{not item.editable}"></h:outputText>
            <h:inputText value="#{item.available}" rendered="#{item.editable}"></h:inputText>
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputText value="Edit"/>
            </f:facet>
            <h:commandLink value="Edit" action="#{gnome.editAction(item)}" rendered="#{not item.editable}" />
        </h:column>
    </h:dataTable>
    <h:commandButton value="Save Changes" action="#{gnome.saveAction}" />
</h:form>

The Bean methods: Bean方法:

public String saveAction() {
    for (Gnome g : productList) {
        g.setEditable(false);
    }
    return null;
}

public String editAction(Gnome gnome) {
    gnome.setEditable(true);
    return null;
}

The property boolean editable in the entity class is annotated as @Transient since it's not a part of the persistant data and I don't want the entity manager to touch it: 实体类中的boolean editable属性被注释为@Transient,因为它不是持久性数据的一部分,并且我不希望实体管理器接触它:

@Transient
private boolean editable;

I thought this would result in the boolean variable being set to true when the Edit-button is clicked, the row re-renders with the fields now as <h:inputText> , but nothing happens. 我认为这将导致在单击“编辑”按钮时将boolean变量设置为true,该行现在以<h:inputText>字段重新呈现,但是什么也没有发生。 I have debugged it and made sure that the variable is set to true, which it is. 我已经调试了它,并确保将变量设置为true。

I also found this SO question that I thought perhaps could be something similar to the problem I am experiencing. 我还发现这太问题 ,我想也许可能是类似我遇到的问题的东西。 The difference though is that a refresh of the page doesn't change anything. 但是区别是页面的刷新不会改变任何东西。

What am I missing? 我想念什么?

Edit 1: In the first version of this question I had by mistake pasted the wrong code. 编辑1: 在此问题的第一个版本中,我错误地粘贴了错误的代码。 The one that is included now is my current code, that in theory should work but does not. 现在包括的是我当前的代码,理论上应该可以,但是不可以。

Edit 2: Initialization of productList , where Gnome is the entity class. 编辑2: productList初始化,其中Gnome是实体类。

private List<Gnome> productList = new ArrayList<>();

public List<Gnome> getProductList() {
    productList = gnomeFacade.findAll();
    return productList;
}

public void setProductList(List<Gnome> productList) {
    this.productList = productList;
}

After testing your example, the only possible error is that you are using a RequestScoped bean. 测试完示例之后,唯一可能的错误是您正在使用RequestScoped bean。 In order to make it work, you need at least ViewScoped or SessionScoped . 为了使其工作,至少需要ViewScopedSessionScoped Ortherwise, the productList changed content is lost at every requests (button action). 否则, productList更改的内容在每次请求时都会丢失(按钮操作)。

According to the edit, the second mistake is how data is took. 根据编辑,第二个错误是如何获取数据。 You need to load your data somewhere else than getter like this : 您需要像getter这样将数据加载到其他地方:

@PostConstruct
public void init()
{
    productList = gnomeFacade.findAll();
}

public List<Gnome> getProductList()
{
    return productList;
}

Ortherwise, at every actions your list is reset. 否则,将在每次操作时重置您的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM