note

struts2 itertator 본문

JSP/Struts2

struts2 itertator

투한 2012. 2. 23. 12:13









com.ch7.domain/Board.java
package com.ch7.domain;

public class Board {
	private int no;
	private String subject;
	private String name;
	private String content;
	
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
}



IteratorAction.java
package com.ch7.action;

import java.util.ArrayList;
import java.util.List;

import com.ch7.domain.Board;
import com.opensymphony.xwork2.Action;

public class IteratorAction implements Action{

	private List<Board>list;

	@Override
	public String execute() throws Exception {
		list = new ArrayList<Board>();
		
		Board board = new Board();
		board.setNo(1);
		board.setSubject("첫 방문");
		board.setName("우허허");
		board.setContent("게시판 오픈 ㅊㅋ");
		list.add(board);
		
		Board board2 = new Board();
		board2.setNo(2);
		board2.setSubject("송년회");
		board2.setName("개구리");
		board2.setContent("잡담");
		list.add(board2);
		
		Board board3 = new Board();
		board3.setNo(3);
		board3.setSubject("졸업작품");
		board3.setName("팥쥐");
		board3.setContent("월드컵과 올림픽을 형상화");
		list.add(board3);
		
		
		return SUCCESS;
	}

	public List<Board> getList() {
		return list;
	}
}



Iterator.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>iterator Test</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="1" width="100%" align="center">
<tr>
	<td>번호</td>
	<td>제목</td>
	<td>글쓴이</td>
	<td>내용</td>
</tr>
	<s:iterator value="list">
	<tr>
		<td><s:property value="no" /></td>
		<td><s:property value="subject" /></td>
		<td><s:property value="name" /></td>
		<td><s:property value="content" /></td>
	</tr>
	</s:iterator>
</table>
</body>
</html>

오히려 EL표기법 foreach보다 편하다
[JSP/JSTL] - EL , JSTL 코어태그 (c:forEach)(c:choose)