2011-09-01 27 views
1

JSF 2.0(mojarra)應用程序。我有一個非常平凡的形式將商品提交JSF2表單不會在同一頁上重新加載集合

<h:form> 
    #{msg['add custom title']}:<br /> 
    <table> 
     <tr> 
      <td>#{msg['heaading']}:</td> 
      <td><h:inputText value="#{titlesBean.title.heading}"></h:inputText></td> 
     </tr> 
     <tr> 
          ... 
     </tr> 
    </table> 
    <h:commandButton action="#{titlesBean.addTitle}" value="#{msg['g.save']}" /> 
</h:form> 

,然後在同一頁面上我已經添加的所有物品的清單:

<h:dataTable id="manualTitlesForm" value="#{titlesBean.manualTitles}" var="title" border="1" cellspacing="0"> 
    <h:column> 
     <f:facet name="header">#{msg['heaading']}</f:facet> 
     #{title.heading} 
    </h:column> 
      ... 
    <h:column> 
     <f:facet name="header">#{msg['actions']}</f:facet> 
     <h:form> 
      <h:commandButton action="#{titlesBean.editManualTitle(title)}" value="#{msg['g.edit']}" /> 
      <h:commandButton action="#{titlesBean.deleteManualTitle(title.id)}" value="#{msg['g.delete']}" /> 
     </h:form> 
    </h:column> 
</h:dataTable> 

在bean代碼的代碼是超級簡單:

@Controller 
@Scope(Scopes.REQUEST) 
public class TitlesBean { 

    private List<JTitle> manualTitles; 


    @PostConstruct 
    private void init() { 
     this.manualTitles = titlesManager.getManualTitles(); 
    } 

    public String addTitle() { 
     title.setCreated(new Date()); 
     title.setManual(true); 
     try { 
      titlesManager.addTitle(title); 
      title = new JTitle();// this is added, delete from the variable. only if no exception though !!! 
      UserMessagesBean.addMessage("saved"); 
     } catch (Exception e) { 
      UserMessagesBean.setValidationException(e.getMessage());//different exception added 
     } 
     return null; 
    } 

    public List<JTitle> getManualTitles() { 
     return manualTitles; 
    } 
    } 

現在的問題是,getManualTitles()被稱爲多次冠軍的次數我有,這會導致例如12個調用數據庫,而不是一個。爲什麼這種情況超出我的理解。我可以通過在bean中緩存手冊標題來解決這個問題。這不是我的主要問題。

的問題是,addTitle()被調用後getManualTitles()。實際上getManualTitles()被稱爲例如10次,然後是addTitle(),然後是getManualTitles()方法的兩倍多。這讓我覺得這是一種平行執行,它使我的頁面只顯示12箇舊記錄而不是13個。我必須重新加載頁面,然後顯示13。

更新:現在緩存列表。問題仍未解決。

爲什麼?我怎樣才能解決這個問題?

+1

可能重複的[在數據表中更新輔助bean從h:命令按鈕和JPA](http://stackoverflow.com/questions/7221758/update-backing-bean-in-datatable-from -hcommandbutton-and-jpa) – BalusC

+0

這回答了對數據庫的多次調用的問題,但是這怎麼解決我的主要問題? – mist

+1

這也包含在答案中的代碼示例中。 – BalusC

回答

-1

這是速戰速決,而不是一個真正的解決方案。重定向的addTitle()結果:

添加以下addTitle()

... 
    FacesContext.getCurrentInstance().getExternalContext() 
     .redirect("manualTitles.jsf"); 
    return null; 
} 
+0

你的問題是你正在做一個getter方法的商業工作!另請參閱重複的鏈接。 – BalusC

+0

好吧,忽略那個部分,認爲它已經解決了,這能解決我的其他問題嗎? – mist

相關問題