【问题标题】:PropertyNotFoundException when calling valueChangeListner调用 valueChangeListner 时出现 PropertyNotFoundException
【发布时间】:2016-11-22 21:55:59
【问题描述】:

我有以下简单的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>

当我在shoppingcenterControler 上使用@Named 注释时,我收到javax.el.PropertyNotFoundException 警告我Target Unreachable, identifier 'shoppingcenterControler' resolved to null。

当我使用 @ManagedBean 注释时,我收到警告:Property 'shopChooseAction' not found on type com.manageMyShopping.presentation.controler.ShoppingcenterControler,而 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。

【问题讨论】:

    标签: java eclipse spring maven propertynotfoundexception


    【解决方案1】:

    我终于解决了我的问题。我没有使用valueChangeListener 作为标签&lt;h:selectOneMenu&gt; 的属性,而是使用了标签&lt;f:valueChangeListener&gt;,如下所示:

    <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 注释没有按预期运行。

    【讨论】:

      猜你喜欢
      • 2017-08-12
      • 2015-07-01
      • 2016-07-27
      • 2012-08-27
      • 2015-03-02
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多