2012-04-09 50 views
0

使用JSF + PrettyFaces處理導航和bean操作時,Java Web App出現問題。JSF2 <h:selectOneMenu和<f:在PrettyFaces過濾器導航後未調用的<ajax偵聽器

使用的版本:

JSF - 2.1
PrettyFaces - 使用PrettyFaces從鏈接過濾處理導航時3.3.3

錯誤發生的情況:

   <div class="product_img"> 
       <pretty:link mappingId="viewProduct"> 
        <img src="#{request.contextPath}/image?id=#{product.mainImage.fileReference.id}" />            
        <f:param value="#{productMB.filters.productCategoryName}" /> 
        <f:param value="#{product.name}" />                   
       </pretty:link>             
      </div>               

漂亮,配置.xml映射是:

<url-mapping id="viewProduct"> 
     <pattern value="/shop/product/#{ productCategoryName : productMB.filters.productCategoryName }/#{ productName : productMB.filters.productName }/" /> 
     <view-id value="/pages/productDetails.faces" /> 
     <action>#{productMB.openProductDetails}</action> 
    </url-mapping> 

豆動作:

public String openProductDetails() { 

    if (filters.getProductCategoryName() != null && !filters.getProductName().equals("")) {   
     loadProductDetailsByName(filters.getProductCategoryName()); 
    } 

    return "/pages/productDetails.faces"; 
} 

在產品詳細信息頁用戶土地,在那裏他可以選擇的產品特點如顏色,大小等...

<ui:fragment rendered="#{productMB.productVO.featureColorAvailable}"> 
     <span class="productFeatureLabel">#{msg.color}</span> 

     <h:selectOneMenu id="colorSelect" 
      value="#{productMB.productVO.colorSelected}" 
      valueChangeListener="#{productMB.colorSelectedValueChanged}"> 
      <f:selectItem itemLabel="#{msg['select']}" noSelectionOption="true" /> 
      <f:selectItems value="#{productMB.productFeatureAvailableApplMap['color']}" var="colorItem" itemValue="#{colorItem}" 
       itemLabel="#{msg[colorItem.name]}" /> 
      <f:ajax event="valueChange"    
      listener="#{productMB.colorSelectedValueChanged}" 
       render="@this productDetailImage productSubImage productSubImage2 productSubImage3 sizeSelect"></f:ajax> 
     </h:selectOneMenu>  
    </ui:fragment> 

// ...豆類動作方法

public void colorSelectedValueChanged(ValueChangeEvent event) { 

     if (null != event.getNewValue()) { 
      ProductFeatureAppl prodFeatureAppl = (ProductFeatureAppl) event.getNewValue(); 
      filterProductFeatureSelectItem(AppConstants.SIZE, prodFeatureAppl.getProductFeature().getName()); 
     } else { 
      filterProductFeatureSelectItem(AppConstants.SIZE, null); 
     } 
    } 

    public void colorSelectedValueChanged(AjaxBehaviorEvent event) throws javax.faces.event.AbortProcessingException { 

     try { 
      if (null != productVO.getColorSelected()) { 
       ProductFeatureAppl prodFeatureAppl = productVO.getColorSelected(); 
       filterProductFeatureSelectItem(AppConstants.SIZE, prodFeatureAppl.getProductFeature().getName()); 

       String prodFeatName = prodFeatureAppl.getProductFeature().getName(); 

       // switch selected pics. 
       productVO.setCurrentDetailImageName(productVO.getProduct().getProductImageByTypeAndFeatureName(
         NameConstants.DETAIL, prodFeatName).getFileReference().getId()); 

       productVO.setCurrentBigImageName(productVO.getProduct().getProductImageByTypeAndFeatureName(
         NameConstants.BIG, prodFeatName).getFileReference().getId()); 

      } else { 
       filterProductFeatureSelectItem(AppConstants.SIZE, null); 
       filterProductFeatureSelectItem(AppConstants.COLOR, null); 

       // reset to default 
       productVO.setCurrentDetailImageName(productVO.getProduct().getProductImageByType(ProductImageType.DETAIL1.toString()).getFileReference().getId()); 
       productVO.setCurrentBigImageName(productVO.getProduct().getProductImageByType(ProductImageType.BIG1.toString()).getFileReference().getId());             
      }  

     } catch (Exception ex) { 
      addErrorMessage(getMessage("error")); 
      if (screenComponent.isDisplayException()) { 
       addErrorMessage(ex.getMessage()); 
      } 
     } 

    } 

第一導航,產品詳細信息頁面之後,每當選擇從 selectOneMenu用於ID = 「colorSelect」 的valueChangeListener和AJAX聽者的值不被調用。除此之外,調用productMB.openProductDetails操作。

ajax調用完成,沒有錯誤,我可以在日誌中看到,但沒有監聽器方法被調用。 有人知道爲什麼這些JSF Ajax偵聽器被跳過嗎?

在此先感謝。

回答

1

如果您直接從URL訪問頁面,頁面是否可以正常工作?如果PrettyFaces被禁用,它會起作用嗎?

我懷疑這是鏈接或PrettyFaces的問題。我敢打賭,它與AJAX和部分狀態保存有關。

這可能是因爲它與此有關:

public String openProductDetails() { 
    if (filters.getProductCategoryName() 
      != null && !filters.getProductName().equals("")) {   
     loadProductDetailsByName(filters.getProductCategoryName()); 
    } 

    return "/pages/productDetails.faces"; // why is this being returned? 
} 

你在那裏返回頁面名稱。 PrettyFaces實際上可能試圖在這個String上導航。如果您希望顯示您在映射中配置的頁面,只需返回null,返回空字符串或不返回任何內容。

希望這會有所幫助。

+0

我改變了方法返回null;現在調用監聽器,但仍在HTTP POST POST Ajax請求的JSF生命週期中調用productMB.openProductDetails()。我試着改變:\t \t Pretty Filter \t com.ocpsoft.pretty.PrettyFilter \t \t false \t \t \t 爲true和false,但都沒有解決。我還嘗試更改PrettyFilter的過濾器映射,以包含ASYNC和INCLUDE的調度程序,但添加這些或刪除並不能解決問題。 – guilhebl 2012-04-10 20:33:18

+0

我發現productMB.openProductDetails在任何ajax請求之後被調用的原因,我添加了#{productMB.openProductDetails}。我相信我將不得不對所有具有ajax組件的頁面使用onPostback = false。你知道這是否是關於Ajax調用和PrettyFaces的正確說法嗎?提前致謝。 – guilhebl 2012-04-10 21:11:14

+0

是的,這是正確的,因爲JSF AJAX請求實際上使用HTTP POST。 – Lincoln 2012-04-12 21:19:22