PropertyNotFoundException调用valueChangeListner
问题描述:
时,我有以下简单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
然而,它既不适合我,也不喜欢假的解决方案。我在寻找一个真正的解决方案,我想了解
- 为什么因为预计
@Named
注释不能正常工作?我已将相应的依赖项添加到我的项目的pom.xml
文件中。 - 为什么
valueChnageListener
应该提出PropertyNotFoundException
这个方法的名字?
任何帮助,高度赞赏。
我的环境:Fedora 24,Java 1.8,apache-tomcat 8.0.33,我正在使用Eclipse Mars。
答
我终于解决了我的问题。代替使用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
注释不能如预期那样工作。