javax.el.E​​LException:无法设置属性“propertyName”在类“com.example.MrBean”上值“null”

发布于 2024-12-12 04:19:26 字数 4516 浏览 0 评论 0原文

在我的 .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>

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

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:感谢大家对我的帮助!问题是我愚蠢地忘记将 标签放入 标签内。

In my .xhtml file, I have the following SelectOneMenu component:

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

When I open the page, it can load normally with the menu populated properly. However, when I choose 1 of the option, I ran into the following error:

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

EDIT: this is my bean with getListOfItems function:

@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; 
    }
    ...
}

I'd be very grateful if someone could give me an advice on how to tackle this problem.

EDIT 2: Thanks everyone for helping me! The problem was that I stupidly forgot to put the <f:ajax> tag inside a <h:form> tag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森末i 2024-12-19 04:19:26

这是我尝试过的:

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

with:

<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) 版本,或者您在其他地方遇到了问题。

Here is what I tried:

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

with:

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

And it works without any problem.

Might be the version of JSF (EL) you have, or you have problem somewhere else.

北方。的韩爷 2024-12-19 04:19:26

标签需要包装在 标签内。

The <f:ajax> tag need to be wrapped inside a <h:form> tag.

左耳近心 2024-12-19 04:19:26

似乎您的某些项目值为 null。如果您想接受 null,请使用 Long 而不是 long

Seems like some of your item values are null. If you want to accept null, use Long instead of long.

甜心 2024-12-19 04:19:26

我遇到了同样的问题,通过将变量类型 boolean 更改为 Boolean 解决了这个问题

I had the same issue and it was resolved by changing my variable type boolean to Boolean

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文