코딩 관련/Spring 관련
[Spring] vo 여러개 전달하기. LIST<VO> vo를 list 로 넘기기 / 받기
메리짱123
2020. 10. 8. 15:54
반응형
JSP Form에서 VO가 여러개로, 즉 list로 넘기고 controller에서 받아야 하는 경우
List를 사용하면 됨
1. VO객체
BoardVo 클래스 안에 List<BoardVO>를 선언하고 getter와 setter도 만들었음!
public class BoardVo {
private String boardType;
private int boardNum;
private String boardTitle;
private String boardComment;
private String creator;
private String modifier;
private int totalCnt;
private List<BoardVo> boardVoList;
public List<BoardVo> getBoardVoList() {
return boardVoList;
}
public void setBoardVoList(List<BoardVo> boardVoList) {
this.boardVoList = boardVoList;
}
2. JSP의 Form
name은 List객체이름[index].property 이름
<form:form commandName="boardVo" class="boardWrite" action="/board/boardWriteAction.do">
<select name="boardVoList[0].boardType" >
<c:forEach items="${codeList}" var="list">
<option value="${list.codeId}">${list.codeName}</option>
</c:forEach>
</select>
<input name="boardVoList[0].boardTitle" type="text" size="50" value="${board.boardTitle}" maxlength="24">
반응형
3. Controller
파라미터를 전달받을땐 List가 아닌 Vo로 받음
//글등록 수행
@RequestMapping(value = "/board/boardWriteAction.do", method = RequestMethod.POST)
@ResponseBody
public String boardWriteAction(Locale locale, BoardVo boardVo) throws Exception{
HashMap<String, String> result = new HashMap<String, String>();
CommonUtil commonUtil = new CommonUtil();
int resultCnt = 0;
System.out.println(boardVo.getBoardVoList().get(0).getBoardType());
System.out.println(boardVo.getBoardVoList().get(0).getBoardTitle());
System.out.println(boardVo.getBoardVoList().get(0).getCreator());
resultCnt=boardService.boardInsert(boardVo);
List에 담긴 값(프로퍼티) 호출 시에는
Vo이름.get리스트이름().get(index).get프로퍼티()
4. Service Impl
List를 호출할 때는 vo이름.getlist이름().get(인덱스) 해서 사용하면 됨!
@Override
public int boardInsert(BoardVo boardVo) throws Exception {
int[] results= new int[boardVo.getBoardVoList().size()];
int result=1;
for(int i=0; i<boardVo.getBoardVoList().size();i++) {
results[i]=boardDao.boardInsert(boardVo.getBoardVoList().get(i));
result *= results[i];
}
return result;
}
반응형