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>

entryPageBeanのコード:

@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は実行されますが(出力にログメッセージが表示されます)、ラベルは更新されません。

ボタンを押したときにラベルが更新されるようにコードを変更するにはどうすればよいですか?

4

1 に答える 1

4

新しい答え:

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

コマンドボタンはデフォルトでajaxedされているためです。

次に、Beanを@ViewScopedにする必要があります。次に、yearLabelの値をyearに割り当てる必要があります。

 @PostConstruct
public void init()
{
    this.yearLabel = "2012";
    this.year = Integer.parseInt(yearLabel);
}
于 2012-10-21T09:09:56.687 に答える