Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在弹簧中保存多个对象<;表格:表格>; @组件 @实体 @表(name=“菜单”) @可配置 公共类菜单实现可序列化{ .... @OneToMany(mappedBy=“menu”,fetch=FetchType.EAGER) 私人录音室; 公共集getVocementList(){ 返回叫喊者; } 公共void setvocementulist(Set vocementulist){ this.voceMenuList=voceMenuList; } ..... }_Java_Spring_Jsp_Jstl_Spring Form - Fatal编程技术网

Java 如何在弹簧中保存多个对象<;表格:表格>; @组件 @实体 @表(name=“菜单”) @可配置 公共类菜单实现可序列化{ .... @OneToMany(mappedBy=“menu”,fetch=FetchType.EAGER) 私人录音室; 公共集getVocementList(){ 返回叫喊者; } 公共void setvocementulist(Set vocementulist){ this.voceMenuList=voceMenuList; } ..... }

Java 如何在弹簧中保存多个对象<;表格:表格>; @组件 @实体 @表(name=“菜单”) @可配置 公共类菜单实现可序列化{ .... @OneToMany(mappedBy=“menu”,fetch=FetchType.EAGER) 私人录音室; 公共集getVocementList(){ 返回叫喊者; } 公共void setvocementulist(Set vocementulist){ this.voceMenuList=voceMenuList; } ..... },java,spring,jsp,jstl,spring-form,Java,Spring,Jsp,Jstl,Spring Form,我打印表单以编辑菜单及其相关VoceMenu对象,方法如下: @Component @Entity @Table(name="menu") @Configurable public class Menu implements Serializable{ .... @OneToMany(mappedBy="menu", fetch=FetchType.EAGER) private Set<VoceMenu> voceMenuList;

我打印表单以编辑菜单及其相关VoceMenu对象,方法如下:

@Component
@Entity
@Table(name="menu")
@Configurable
public class Menu implements Serializable{      
    ....        
    @OneToMany(mappedBy="menu", fetch=FetchType.EAGER)
    private Set<VoceMenu> voceMenuList; 

    public Set<VoceMenu> getVoceMenuList() {
        return voceMenuList;
    }

    public void setVoceMenuList(Set<VoceMenu> voceMenuList) {
        this.voceMenuList = voceMenuList;
    }
    .....   
}

菜单id
...... .....
但是,当我尝试保存“对象”菜单时,出现以下错误:

bean类[com.springgestionErrori.model.Menu]的无效属性“vocementList[0]”:无法 从大小为0的集合中获取索引为0的元素,
使用属性路径“voceMenuList[0]”访问集。

无法通过索引访问集的元素。您需要添加一些方法,这些方法返回一个包装集合的列表

<form:form action="editMenu" method="post" commandName="menu"> 
     Menu id<form:input path="id" maxlength="11"/><br/>       
     ...... 
    <c:forEach items="${menu.voceMenuList}" varStatus="counter">            
        <form:input path="voceMenuList[${counter.index}].id" maxlength="11"/>
             .....
    </c:forEach>
    <input type="submit">
</form:form>
@组件
@实体
@表(name=“菜单”)
@可配置
公共类菜单实现可序列化{
....        
@OneToMany(mappedBy=“menu”,fetch=FetchType.EAGER)
私人设置的voceMenus;
公共集getVoceMenus(){
返回voceMenus;
}
公共无效集合voceMenus(集合voceMenus){
this.voceMenus=voceMenus;
}
//受此约束
公共列表getVocementUsalsList(){
返回新的ArrayList(voceMenus);
}
.....   
}
JSP:


菜单id
...... .....
请您发布控制器代码好吗?您原来的问题已经得到了回答。对于由完全独立的问题引起的任何后续问题,您应该提出一个新问题。好的,谢谢您的帮助我根据您的帮助编辑了我的代码。现在我得到一个不同的错误。我不知道我是否没有正确地遵循您的建议您确定传递给ArrayList构造函数的集合不为null吗?我编辑了这行代码私有集合vocementList=new HashSet();现在我没有得到任何错误,但是对象菜单的字段vocementList的大小始终为0。我想知道我是否必须为vocemenualsist添加一个set方法您希望由JPA层填充的set为null,这就是它失败的原因。通过添加此语句,可以防止其为空。为什么不从JPA层填充它是另一个问题。如果在get as list方法中,手动添加一些VoceMenu项,那么这应该确认您所拥有的内容按预期工作,并且问题在JPA层中。如果是这样的话,那么你原来的问题是固定的,你应该标记答案为已接受,并围绕JPA问题提出一个新的问题。好的,我可以知道如何包括这种方法的setter吗?我看到没有添加值。
@Component
@Entity
@Table(name="menu")
@Configurable
public class Menu implements Serializable{      
    ....        
    @OneToMany(mappedBy="menu", fetch=FetchType.EAGER)
    private Set<VoceMenu> voceMenus; 

    public Set<VoceMenu> getVoceMenus() {
        return voceMenus;
    }

    public void setVoceMenus(Set<VoceMenu> voceMenus) {
        this.voceMenus = voceMenus;
    }

    //bind to this
    public List<VoceMenu> getVoceMenusAsList(){
        return new ArrayList<VoceMenu>(voceMenus);
    }
    .....   
}
<form:form action="editMenu" method="post" commandName="menu"> 
     Menu id<form:input path="id" maxlength="11"/><br/>       
     ...... 
    <c:forEach items="${menu.voceMenusAsList}" varStatus="counter">            
        <form:input path="voceMenusAsList[${counter.index}].id" maxlength="11"/>
             .....
    </c:forEach>
    <input type="submit">
</form:form>