2016-07-19 85 views
0

時,我有以下簡單selectOneMenu用於PropertyNotFoundException調用valueChangeListner

<h:selectOneMenu id="shop" styleClass="tcell" 
       value="#{shoppingcenterControler.shoppingCenterScreenBean.shoppingcenterName}" 
       onchange="submit()" 
       valueChangeListener="#{shoppingcenterControler.shopChooseAction}"> 
    <f:selectItem itemValue="#{option.defaultShoppingcenter}" itemLabel="#{option.defaultShoppingcenter}"></f:selectItem> 
    <f:selectItems value="#{shoppingCenterScreenBean.shoppingcenternames}"></f:selectItems> 
</h:selectOneMenu> 

當我使用@Named註釋上shoppingcenterControler我收到javax.el.PropertyNotFoundException警告我Target Unreachable, identifier 'shoppingcenterControler' resolved to null

當我使用@ManagedBean註解我收到警告:Property 'shopChooseAction' not found on type com.manageMyShopping.presentation.controler.ShoppingcenterControler,whlie shopChooseAction不是屬性,它是:

public void shopChooseAction(ValueChangeEvent event){ 
    String shopName = getShoppingCenterScreenBean().getShoppingcenterName(); 
    if (!shopName.equals(defaultShopp)) { 
     for (ShoppingCenterScreenBean thisShop : Shoppinglistcontroler.getShoppinglistScreenBean().getShoppingCentersScreenBean()) { 
      if (!thisShop.getShoppingcenterName().equals(shopName)) { 

       ShoppingCenterScreenBean newShoppingcenter = new ShoppingCenterScreenBean(); 
       newShoppingcenter.setShoppingcenterName(shopName); 
       ShoppinglistScreenBean shoppinglist = Shoppinglistcontroler.getShoppinglistScreenBean(); 
       shoppinglist.getShoppingCentersScreenBean().add(newShoppingcenter); 
      } 
     } 
    } 
} 

我看過不同的環節,包括以下內容: One somehow similar question

然而,它既不適合我,也不喜歡假的解決方案。我在尋找一個真正的解決方案,我想了解

  1. 爲什麼因爲預計@Named註釋不能正常工作?我已將相應的依賴項添加到我的項目的pom.xml文件中。
  2. 爲什麼valueChnageListener應該提出PropertyNotFoundException這個方法的名字?

任何幫助,高度讚賞。

我的環境:Fedora 24,Java 1.8,apache-tomcat 8.0.33,我正在使用Eclipse Mars。

回答

0

我終於解決了我的問題。代替使用valueChangeListener作爲標籤<h:selectOneMenu>屬性的我已經使用了標籤<f:valueChangeListener>如下:

<h:selectOneMenu id="shop" styleClass="tcell" 
           value="#{shoppingcenterControler.shoppingCenterScreenBean.shoppingcenterName}" 
           onchange="submit()"> 
           <f:selectItem itemValue="#{option.defaultShoppingcenter}" itemLabel="#{option.defaultShoppingcenter}"></f:selectItem> 
           <f:selectItems 
            value="#{shoppingCenterScreenBean.shoppingcenternames}"></f:selectItems> 
           <f:valueChangeListener type="com.manageMyShopping.presentation.controler.ShoppingcenterListener"></f:valueChangeListener> 
</h:selectOneMenu> 

和我已經添加了類ShoppingcenterListener爲:

public class ShoppingcenterListener implements ValueChangeListener { 

FacesContext context = FacesContext.getCurrentInstance(); 
String bundleName = "com.manageMyShopping.presentation.elementaryBeans.itemOptions"; 
Locale local = context.getViewRoot().getLocale(); 
ResourceBundle bundle = ResourceBundle.getBundle(bundleName, local); 
String defaultShop = bundle.getString("defaultShoppingcenter"); 

@Override 
public void processValueChange(ValueChangeEvent event) throws AbortProcessingException { 

    String shopName = event.getNewValue().toString(); 
    System.out.printf("ShoppingcenterNqme is: %s\n", shopName); 
    if (!shopName.equals(defaultShop)) { 
     for (ShoppingCenterScreenBean thisShop : Shoppinglistcontroler.getShoppinglistScreenBean().getShoppingCentersScreenBean()) { 
      if (!thisShop.getShoppingcenterName().equals(shopName)) { 

       ShoppingCenterScreenBean newShoppingcenter = new ShoppingCenterScreenBean(); 
       newShoppingcenter.setShoppingcenterName(shopName); 
       ShoppinglistScreenBean shoppinglist = Shoppinglistcontroler.getShoppinglistScreenBean(); 
       shoppinglist.getShoppingCentersScreenBean().add(newShoppingcenter); 
      } 
     } 
    } 
} 

}

這解決了我的問題。但我不明白爲什麼@Named註釋不能如預期那樣工作。

相關問題