[英]Update a label in JSF (Primefaces)
I have a JSF page with a button and a label. 我有一个带按钮和标签的JSF页面。 When the user presses the
simulateYearButton
button, the value displayed in the label yearLabel
should be incremented. 当用户按下
simulateYearButton
按钮时,标签yearLabel
显示的值应递增。
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>
Code of the entryPage
bean: 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;
}
}
When I press the simulateYearButton
, the method simulateYearButtonAction
is executed (I see the log message in the output), but the label is not updated. 当我按下
simulateYearButton
,执行方法simulateYearButtonAction
(我在输出中看到日志消息),但标签没有更新。
How should I modify the code so that the label is updated, when I press the button? 当我按下按钮时,我应该如何修改代码以便更新标签?
New answer: 新答案:
<p:commandButton value="Просимулировать год" id="simulateYearButton"
actionListener="#{entryPage.simulateYearButtonAction}" update="yearLabel">
</p:commandButton>
Because commandbutton is ajaxed by default. 因为commandbutton默认是ajaxed。
Next, you need to make the bean @ViewScoped. 接下来,您需要创建bean @ViewScoped。 Next, you need to assign to year the value of yearLabel :
接下来,您需要为year指定yearLabel的值:
@PostConstruct
public void init()
{
this.yearLabel = "2012";
this.year = Integer.parseInt(yearLabel);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.