javax.el.E​​LException:无法将类'com.example.MrBean'上的属性'propertyName'设置为值'null'

时间:2011-10-26 13:25:23

标签: jsf-2 el

在我的.xhtml文件中,我有以下SelectOneMenu组件:

<ui:define name="formContent">
    <h:selectOneMenu value="#{mrBean.itemCategoryID}">
        <f:ajax render="abc def" execute="@this" listener="#{mrBean.getListOfItems}"></f:ajax> 
        <f:selectItem itemLabel="Choose one .." itemValue="0" noSelectionOption="true" />
        <f:selectItems value="#{mrBean.itemCategories}" var="ic"
                       itemLabel="#{ic.name}" itemValue="#{ic.id}" />
    </h:selectOneMenu>

<h:panelGrid id="abc" columns="3" border="1">
    <h:outputText style="font-weight: bold" value="Name"/> 
    <h:outputText style="font-weight: bold" value="Producer" />
    <h:outputText />
</h:panelGrid>    

<h:panelGroup id="def" >
    <ui:repeat value="#{mrBean.items}" var="i">
        <h:form id="BuyItemForm">
            <h:panelGrid columns="3" border="1">
                <h:outputText style="font-weight: normal" value="#{i.name}" /> 
                <h:outputText style="font-weight: normal" value="#{i.producer.name}" /> 
                <h:commandButton value="Buy" actionListener="#{mrBean.buyItem}" >
                    <f:param name="itemID" value="#{i.id}" />
                </h:commandButton> 

            </h:panelGrid>
        </h:form>
    </ui:repeat>
</h:panelGroup>
</ui:define>

当我打开页面时,它可以正常加载,并正确填充菜单。但是,当我选择1选项时,我遇到了以下错误:

SEVERE: javax.faces.component.UpdateModelException: javax.el.ELException: /partner/BuyItem.xhtml @53,81 value="#{mrBean.itemCategoryID}": Can't set property 'itemCategoryID' on class 'managedBean.MrBean' to value 'null'.
... 
Caused by: javax.el.ELException: /partner/BuyItem.xhtml @53,81 value="#{mrBean.itemCategoryID}": Can't set property 'itemCategoryID' on class 'managedBean.MrBean' to value 'null'.
    at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:139)
    at javax.faces.component.UIInput.updateModel(UIInput.java:818)
    ... 47 more
Caused by: java.lang.IllegalArgumentException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.el.BeanELResolver.setValue(BeanELResolver.java:381)
    at com.sun.faces.el.DemuxCompositeELResolver._setValue(DemuxCompositeELResolver.java:255)
    at com.sun.faces.el.DemuxCompositeELResolver.setValue(DemuxCompositeELResolver.java:281)
    at com.sun.el.parser.AstValue.setValue(AstValue.java:197)
    at com.sun.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:286)
    at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:131)
    ... 48 more

编辑:这是我的带有getListOfItems函数的bean:

@ManagedBean
@ViewScoped
public class MrBean {
    @EJB
    private PartnerBeanLocal partnerBean;

    @ManagedProperty(value="0")
    private long    itemCategoryID;
    private List<ItemState> items;

    ...
    public void getListOfItems() {
        try {
            System.out.println(itemCategoryID); // I never saw this line printed out
            ArrayList data = partnerBean.getInfo(Constants.GET_LIST_OF_ITEMS, itemCategoryID);

            int result = ((Integer) data.get(0)).intValue();
            if (result == Constants.STATUS_SUCCESSFUL) items = (List<ItemState>) data.get(1);
            else if (result == Constants.STATUS_NOT_FOUND) FacesContext.getCurrentInstance().getExternalContext().redirect("HomePage.xhtml");

        } catch (IOException ex) {
            Logger.getLogger(MrBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    ...

    // Getters and Setters
    ...
    public long getItemCategoryID() {
        return itemCategoryID;
    }

    public void setItemCategoryID(long itemCategoryID) {
        this.itemCategoryID = itemCategoryID;
    }

    public List<ItemState> getItems() {
        return items; 
    }

    public List<ItemState> setItems(List<ItemState> items) {
        this.items = items; 
    }
    ...
}

如果有人能就如何解决这个问题给我建议,我将非常感激。

编辑2 :谢谢大家的帮助!问题是我愚蠢地忘了将<f:ajax>标记放在<h:form>标记内。

4 个答案:

答案 0 :(得分:1)

以下是我的尝试:

@ManagedBean
@ViewScoped
public class MrBean implements Serializable {
    @ManagedProperty(value="0")
    private long itemCategoryID;
    private List<ItemCategory> itemCategories;

    @PostConstruct
    public void init() {
        this.itemCategories = new ArrayList<ItemCategory>();
        this.itemCategories.add(new ItemCategory("Item1", 1));
        this.itemCategories.add(new ItemCategory("Item2", 2));
        this.itemCategories.add(new ItemCategory("Item3", 3));
    }

    public void getListOfItems() {
        System.out.println(Thread.currentThread().getStackTrace()[1]);
        System.out.println("itemCategoryID: " + this.itemCategoryID);
    }

    public List<ItemCategory> getItemCategories() {
        return itemCategories;
    }

    public void setItemCategories(List<ItemCategory> itemCategories) {
        this.itemCategories = itemCategories;
    }

    public long getItemCategoryID() {
        return itemCategoryID;
    }

    public void setItemCategoryID(long itemCategoryID) {
        this.itemCategoryID = itemCategoryID;
    }
}

使用:

<h:form>
    <h:selectOneMenu value="#{mrBean.itemCategoryID}">
        <f:ajax execute="@this" listener="#{mrBean.getListOfItems}"></f:ajax> 
        <f:selectItem itemLabel="Choose one .." itemValue="0" noSelectionOption="true" />
        <f:selectItems value="#{mrBean.itemCategories}" var="ic"
                           itemLabel="#{ic.name}" itemValue="#{ic.id}" />
    </h:selectOneMenu>
</h:form>

它没有任何问题。

可能是您拥有的JSF(EL)版本,或者您在其他地方遇到问题。

答案 1 :(得分:1)

<f:ajax>代码需要包含在<h:form>代码中。

答案 2 :(得分:0)

您的某些商品值似乎是null。如果您想接受null,请使用Long代替long

答案 3 :(得分:0)

我有同样的问题,它通过将我的变量类型布尔值更改为布尔值

来解决