在JSF中更新标签(Primefaces) [英] Update a label in JSF (Primefaces)

查看:149
本文介绍了在JSF中更新标签(Primefaces)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JSF page with a button and a label. When the user presses the simulateYearButton button, the value displayed in the label yearLabel should be incremented.

带有按钮和标签的表单如下:

The form with the button and the label looks like this:

    <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(我在输出中看到了日志消息),但是标签未更新.

When I press the simulateYearButton, the method simulateYearButtonAction is executed (I see the log message in the output), but the label is not updated.

当我按下按钮时,应该如何修改代码,以便更新标签?

How should I modify the code so that the label is updated, when I press the button?

推荐答案

新答案:

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

因为默认情况下,命令按钮已被取消.

Because commandbutton is ajaxed by default.

接下来,您需要将bean制成@ViewScoped. 接下来,您需要为year分配yearLabel的值:

Next, you need to make the bean @ViewScoped. Next, you need to assign to year the value of yearLabel :

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

这篇关于在JSF中更新标签(Primefaces)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆