2016-07-19 4 views
0

J'ai la selectOneMenu simple suivantePropertyNotFoundException lorsque vous appelez 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> 

Lorsque j'utilise @Named annotation sur shoppingcenterControler je reçois un avertissement me javax.el.PropertyNotFoundExceptionTarget Unreachable, identifier 'shoppingcenterControler' resolved to null.

Lorsque j'utilise l'annotation @ManagedBean Je reçois l'avertissement: Property 'shopChooseAction' not found on type com.manageMyShopping.presentation.controler.ShoppingcenterControler, whlie shopChooseAction est pas une propriété, il est:

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); 
      } 
     } 
    } 
} 

J'ai regardé différents liens dont les suivants: One somehow similar question

Cependant, cela n'a pas fonctionné pour moi, ni j'aime les solutions truquées. Je cherche une vraie solution et je veux comprendre

  1. Pourquoi l'annotation @Named ne fonctionne pas comme prévu? J'ai ajouté la dépendance correspondante au fichier pom.xml de mon projet.
  2. Pourquoi valueChnageListener devrait lever PropertyNotFoundException sur le nom de la méthode?

Toute aide est fortement appréciée.

Mon environnement: Fedora 24, Java 1.8, apache-tomcat 8.0.33, et j'utilise Eclipse Mars.

Répondre

0

J'ai enfin résolu mon problème. Au lieu d'utiliser valueChangeListener comme un attribut pour la balise <h:selectOneMenu> J'ai utilisé la balise <f:valueChangeListener> comme suit:

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

et j'ai ajouté la classe ShoppingcenterListener comme:

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); 
      } 
     } 
    } 
} 

}

et cela a résolu mon problème. Je ne comprends toujours pas pourquoi l'annotation @Named ne fonctionne pas comme elle est supposée.