在JSF(Primefaces)中更新标签

3

我有一个包含按钮和标签的JSF页面。当用户按下simulateYearButton按钮时,显示在标签yearLabel中的值应该增加。

表单与按钮和标签如下所示:

    <h:form>
        <p:commandButton value="Заресетить" id="resetButton"
            actionListener="#{entryPage.resetButtonAction}" />

        <p:commandButton value="Просимулировать год" id="simulateYearButton"
            actionListener="#{entryPage.simulateYearButtonAction}">
            <f:ajax event="click" render="yearLabel" />
        </p:commandButton>


        <h:outputLabel id="yearLabelLabel" for="yearLabel" value="Год:" />
        <h:outputLabel id="yearLabel" value="#{entryPage.yearLabel}"
            style="font-weight:bold" />
    </h:form>

entryPage bean的代码:

@ManagedBean(name = "entryPage")
@RequestScoped
public class EntryPageController {
    ...
    private int year;
    private String yearLabel;

    @PostConstruct
    public void init()
    {
        this.yearLabel = "2012";
    }

    ...

    public void simulateYearButtonAction() {
        LOGGER.debug("EntryPageController.simulateYearButtonAction");
        this.year++;
        this.yearLabel = Integer.toString(this.year);
    }

    public String getYearLabel() {
        return yearLabel;
    }
}

当我按下simulateYearButton时,方法simulateYearButtonAction被执行(我在输出中看到日志消息),但标签没有更新。
我应该如何修改代码,以便在按下按钮时更新标签?
1个回答

4
新答案:
 <p:commandButton value="Просимулировать год" id="simulateYearButton"
      actionListener="#{entryPage.simulateYearButtonAction}" update="yearLabel">
 </p:commandButton>

由于默认情况下,commandbutton 是基于 ajax 的。 接下来,您需要将 bean 设置为 @ViewScoped。 然后,您需要将 year 的值分配为 yearLabel:
 @PostConstruct
public void init()
{
    this.yearLabel = "2012";
    this.year = Integer.parseInt(yearLabel);
}

将其设为ViewScoped,SessionScope仅为一个标签过于浪费资源:-) - Petr Mensik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接