简体   繁体   English

我如何给一个AutopopulatingList引用 <ChildClass> 到清单 <AbstractBaseClass> 在春天?

[英]How do I give reference of a AutopopulatingList<ChildClass> to a List<AbstractBaseClass> in spring?

I am trying to create a dynamically populated list using the AutoPopulatingList. 我正在尝试使用AutoPopulatingList创建一个动态填充的列表。 I also have a form object as shown in the code below: 我还有一个表单对象,如下面的代码所示:

public class CDNoteForm{

    private AbstractCDNote cdnote;
    private List<? extends AbstractInvoiceItem> invoiceItemList ;


    public CDNoteForm() {
        super();
    }
    public CDNoteForm(String type){
        super();
        if(type.equals("cdnote")){
            this.invoiceItemList =  new AutoPopulatingList<CDNoteItem>(CDNoteItem.class);
        }
        else if(type.equals("vendorcdnote")) {          
            this.invoiceItemList =  new AutoPopulatingList<VendorCDNoteItem>(VendorCDNoteItem.class);
        }

    }

    public AbstractCDNote getCdnote() {
        return cdnote;
    }
    public void setCdnote(AbstractCDNote cdnote) {
        this.cdnote = cdnote;
    }
    public List<? extends AbstractInvoiceItem> getInvoiceItemList() {
        return invoiceItemList;
    }
    public void setInvoiceItemList(List<? extends AbstractInvoiceItem> invoiceItemList) {
        this.invoiceItemList = invoiceItemList;
    }
}

The form object is being declared in a method in my controller as shown below. 表单对象在我的控制器中的方法中声明,如下所示。

@RequestMapping(value ="/cdnote/create"  , method = RequestMethod.GET)
    public String getCreateCDNotePage(Model model){

    CDNoteForm cdnoteForm = new CDNoteForm("cdnote");
    model.addAttribute("CDNoteForm" , cdnoteForm);
    return "AddCDNote";
}

The 'invoiceItemList' is being referred in the jsp like this 像这样在jsp中引用“ invoiceItemList”

<form:input type="text" id="Quantity0"  path="invoiceItemList[0].quantity" class="form-control"></form:input>                       

The two classes 'CDNoteItem' and 'VendorCDNoteItem' extend the abstract class 'AbstractInvoiceItem'. “ CDNoteItem”和“ VendorCDNoteItem”这两个类扩展了抽象类“ AbstractInvoiceItem”。 Now I don't get any error on compilation but when I try to run it and visit the page I get the following error on console. 现在我在编译时没有任何错误,但是当我尝试运行它并访问该页面时,在控制台上出现以下错误。

SEVERE: Servlet.service() for servlet [SpringGST] in context with path [/SpringGST] threw exception [org.springframework.beans.InvalidPropertyException: Invalid property 'invoiceItemList[0]' of bean class [com.gst.FormObjects.CDNoteForm]: Illegal attempt to get property 'invoiceItemList' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'invoiceItemList' of bean class [com.gst.FormObjects.CDNoteForm]: Could not instantiate property type [com.gst.models.AbstractInvoiceItem] to auto-grow nested property path: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.gst.models.AbstractInvoiceItem]: Is it an abstract class?; nested exception is java.lang.InstantiationException] with root cause
org.springframework.beans.NullValueInNestedPathException: Invalid property 'invoiceItemList' of bean class [com.gst.FormObjects.CDNoteForm]: Could not instantiate property type [com.gst.models.AbstractInvoiceItem] to auto-grow nested property path: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.gst.models.AbstractInvoiceItem]: Is it an abstract class?; nested exception is java.lang.InstantiationException

I don't understand why it is trying to instantiate the abstract class 'AbstractInvoiceItem' when the 'invoiceItemlist' was initialized with a reference to the autopopulating list of its child class 我不明白为什么在初始化“ invoiceItemlist”并引用其子类的自动填充列表时尝试实例化抽象类“ AbstractInvoiceItem”

您可以使用invoiceItemList.get(0)代替invoiceItemList [0]并尝试

Since invoiceItemList is private, you are getting the exception. 由于invoiceItemList是私有的,因此您将获得异常。 You need to either make it public(not recommended) or you need to use getters and setters to access it. 您需要将其公开(不推荐),或者需要使用getter和setter来访问它。

private List<? extends AbstractInvoiceItem> invoiceItemList ;

public List<? extends AbstractInvoiceItem> getInvoiceItemList(){
  return invoiceItemList;
}
public setInvoiceItemList(List<? extends AbstractInvoiceItem> list){
invoiceItemList= list;
}

Now you can access like this: 现在您可以像这样访问:

<form:input type="text" id="Quantity0"  
path="cdnoteForm.getInvoiceItemList().get(0).quantity" class="form-control">
</form:input>        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM