2012-10-21 46 views
2

我有一個帶有按鈕和標籤的JSF頁面。當用戶按下simulateYearButton按鈕時,標籤yearLabel中顯示的值應遞增。在JSF(Primefaces)中更新標籤

與按鈕和標籤的形式如下:

<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豆:

@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(我見輸出中的日誌消息),但標籤未更新。

我應該如何修改代碼,以便在按下按鈕時更新標籤?

回答

3

新的答案:

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

因爲命令按鈕默認情況下ajaxed。

接下來,您需要創建bean @ViewScoped。 接下來,你需要分配到一年yearLabel值:

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

製作TI ViewScoped,SessionScope巨大矯枉過正只是一個拉貝:-) –

+0

我與彼得同意 –