0

I have the following simple selectOneMenu

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

When I use @Named annotation on shoppingcenterControler I receive a javax.el.PropertyNotFoundException warning me Target Unreachable, identifier 'shoppingcenterControler' resolved to null.

When I use @ManagedBean annotation I receive the warning: Property 'shopChooseAction' not found on type com.manageMyShopping.presentation.controler.ShoppingcenterControler, whlie shopChooseAction is not a property, it is:

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

I have looked different links including the following: One somehow similar question

However, it neither worked for me, nor I do like faked solutions. I am searching a real solution and I want to understand

  1. Why @Named annotation is not functioning as it is expected? I have added the corresponding dependency to the pom.xml file of my project.
  2. Why valueChnageListener should raise PropertyNotFoundException on the name of the method?

Any help is highly appreciated.

My environment: Fedora 24, Java 1.8, apache-tomcat 8.0.33, and I am using Eclipse Mars.

Community
  • 1
  • 1

1 Answers1

0

I have finally solved my problem. Instead of using valueChangeListener as an attribute for the tag <h:selectOneMenu> I have used the tag <f:valueChangeListener> as following:

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

and I have added the class ShoppingcenterListener as:

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

}

and this solved my problem. Still I do not understand why the @Named annotation is not functioning as it is supposed.