簡體   English   中英

無法在表單操作上重新加載JSF頁面

[英]JSF page not reloading on form action

請仔細閱讀,有很多編輯內容
我有這段JSF代碼:

<h:form>
    <h:dataTable class="table-striped" var="_product"
        value="#{productManager.products}"
        border="1"
        binding="#{productManager.table}">
        <h:column>
            <f:facet name="header">Product</f:facet>
            #{_product.name}
        </h:column>
        <h:column>
            <f:facet name="header">Available Units</f:facet>
            #{_product.stock}
        </h:column>
        <h:column>
            <f:facet name="header">Category</f:facet>
            #{_product.category}
        </h:column>
        <h:column>
            <f:facet name="header">Price</f:facet>
            #{_product.price}
        </h:column>
        <h:column>
            <f:facet name="header">Description</f:facet>
            #{_product.description}
        </h:column>
        <h:column>
            <f:facet name="header">Select</f:facet>
            <h:commandButton class="btn btn-primary" value="Select"
                action="#{productManager.selectProduct}"/>
        </h:column>
    </h:dataTable>
</h:form>
<h:form>
    <h:outputLabel for="productName">Selected Product: </h:outputLabel>
    <h:inputText value="#{productManager.selectedDesiredCategory}"/>
    <h:commandButton value="Filter category" action="#{productManager.filterProductsByCategory}"/>
    <h:outputText id="productName" value="#{productManager.selectedName}"/><br/>
    <h:outputLabel for="units">Units: </h:outputLabel>
    <h:inputText id="units" value="#{productManager.selectedUnits}"/>
    <h:commandButton value="Add to basket" action="#{productManager.addToBasket(accountManager.username)}"/><br/>
    <h:outputText rendered="#{productManager.availableMessages}"
        value="#{productManager.message}"/>
</h:form>

#{productManager.filterProductsByCategory}命令#{productManager.filterProductsByCategory}重定向到以下java方法:

public void filterProductsByCategory() {
    this.products = controller.obtainProductListByCategory(selectedDesiredCategory);
    showMessage("Filtered by selected category");
} 

在這里,我使用一組新的按類別過濾的產品來更新products列表的內容,以在視圖中顯示它們。 問題是頁面未重新加載以顯示新內容。 如何實現的?

編輯:showMessage方法實際上在視圖中顯示,因此頁面正在重新加載,但是由於某種原因,該表未更新。 我實際上正在研究查詢返回的數據是否有問題。

編輯:正如我的調試過程所確認的那樣,該查詢返回了良好的結果,但該網頁未正確在表中重新加載數據。

編輯:我發現確實很奇怪。 這是JSF頁面所引用的代碼:

public void filterProductsByCategory()
   {
      filtered = true;
      products = controller.obtainProductListByCategory(selectedDesiredCategory);
      showMessage("Filtered by selected category");
   } 

現在,我使用一個布爾值,實際上知道我什么時候必須提供過濾列表(見為什么在下面的代碼),這是getter的的products列表:

   public List<Product> getProducts()
   {
      if(filtered)
      {
          filtered = false;
          return products;
      }
      products = controller.obtainProductList();
      return products;
   }

在這里,如果是真的,則應僅發送實際的過濾products變量。 但是由於某種原因,它在方法內部反復循環(即使在if內的return語句之后),並將所有乘積再次發送到視圖。 為什么會這樣呢?

默認情況下,JSF調用與視圖中使用的方法相同的getter方法。 例如,對於您的List<Product> products字段及其相應的getter,如果#{productManager.products在您的視圖中兩次出現(即在Facelets代碼中),那么getter也將被執行兩次 因此,托管bean中的getter和setter方法應盡可能干凈,並且不應包含任何涉及的業務邏輯。

基本上,您應該在創建托管bean之后且在視圖呈現時間之前從數據庫中檢索產品列表一次。 為此,您可以使用@PostConstruct批注來裝飾將在創建bean之后執行的void方法。

在代碼中:

@ManagedBean
@ViewScoped
public class ProductManager {
    private List<Product> products;
    @PostConstruct
    public void init() { //you can change the method name
        //if you manually handle the controller, initialize it here
        //otherwise, let it be injected by EJB, Spring, CDI
        //or whichever framework you're working with
        products = controller.obtainProductList();
    }
    public List<Product> getProducts() {
        //plain getter, as simple as this
        //no business logic AT ALL
        return this.products;
    }
    public void filterProductsByCategory() {
        filtered = true;
        products = controller.obtainProductListByCategory(selectedDesiredCategory);
        //I guess this method logs a message or something...
        showMessage("Filtered by selected category");
    }
}

更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM