更新JSF中的标签(Primefaces)

时间:2012-10-21 08:13:50

标签: java jsf primefaces

我有一个带有按钮和标签的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 个答案:

答案 0 :(得分:3)

新答案:

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

因为命令按钮默认为ajaxed。

接下来,您需要创建bean @ViewScoped。 接下来,您需要为year指定yearLabel的值:

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