[SPRING] 봄에 많은 객체를 저장하는 방법 <form : form>
SPRING봄에 많은 객체를 저장하는 방법
@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;
}
.....
}
나는 양식을 인쇄하여 메뉴와 그 상대 VoceMenu 객체를 다음과 같이 편집한다.
<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>
그러나 개체 메뉴를 저장하려고하면이 오류가 발생합니다.
빈 클래스 [com.springgestioneerrori.model.Menu]의 'voceMenuList [0]'속성이 잘못되었습니다. 사이즈 0의 Set로부터 인덱스 0을 가지는 요소를 가져옵니다. 속성 경로 'voceMenuList [0]'을 사용하여 액세스했습니다.
해결법
-
==============================
1.Set의 요소는 인덱스로 액세스 할 수 없습니다. 집합을 래핑하는 List를 반환하는 메서드를 추가해야합니다.
Set의 요소는 인덱스로 액세스 할 수 없습니다. 집합을 래핑하는 List를 반환하는 메서드를 추가해야합니다.
@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); } ..... }
JSP :
<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>
from https://stackoverflow.com/questions/28498216/how-to-save-many-objects-in-a-spring-formform by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] 기본 AOP 프로그램이 BeanCurrentlyInCreationException을 발생시킵니다. (0) | 2019.02.25 |
---|---|
[SPRING] Grails에서 컨트롤러 액션간에 객체를 전달하는 가장 좋은 방법 (0) | 2019.02.25 |
[SPRING] 메이븐 프로젝트에 대한 종속성으로 로컬 non-maven 프로젝트를 추가하는 방법은 무엇입니까? (0) | 2019.02.24 |
[SPRING] 어떻게 스프링 부팅 응용 프로그램에 CSS와 JS를 추가할까요? (0) | 2019.02.24 |
[SPRING] SpringCache를 통해 캐시 된 중첩 된 작업 캐싱 (0) | 2019.02.24 |