-------------------------------------------/WEB-INF/struts-config.xml
<action
path="/List"
type="board.action.ListAction">
<forward name="list" path="/list.jsp"/>
</action>
-------------------------------------------ListAction.java
package board.action;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import board.dao.BoardDAO;
import board.form.BoardUtilForm;
public class ListAction extends Action{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
BoardDAO boardDao=new BoardDAO();
Connection conn=getConnection();
int offset=0;
String offset_get=request.getParameter("offset");
if(offset_get==null){
offset=0;
}
else{
offset=Integer.parseInt(offset_get);
}
int limit=10;
int rcnt=0;
List list=null;
BoardUtilForm boardUtilForm=null;
try{
rcnt=boardDao.boardCount(conn);
request.setAttribute("rcnt", rcnt);
list=boardDao.getBoardList(offset,limit,conn);
}catch(Exception e){
throw new ServletException(e);
}finally{
if(conn!=null) try { conn.close(); } catch(SQLException e){}
}
request.setAttribute("list", list);
return mapping.findForward("list");
}
private Connection getConnection() throws Exception{
Context initCtx=new InitialContext();
DataSource ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/oracle");
return ds.getConnection();
}
}
-------------------------------------------BoardDAO.java 추가
public List getBoardList(int offset,int limit,Connection conn) throws SQLException{
Statement stat=null;
ResultSet rs=null;
String sql=null;
List boardList=new ArrayList();
try{
stat=conn.createStatement();
sql=" Select a.* ";
sql+=" From ( ";
sql+=" Select ROWNUM as RNUM, b.* ";
sql+=" FROM ( ";
sql+=" Select * From board2 Order By idx desc ";
sql+=" ) b ";
sql+=" ) a ";
sql+=" Where a.RNUM >"+offset+" ";
sql+=" And a.RNUM<="+(offset+limit)+" ";
rs=stat.executeQuery(sql);
while(rs.next()){
BoardDTO BoardDto=new BoardDTO(
rs.getInt("idx"),
rs.getString("name"),
rs.getString("email"),
rs.getString("homepage"),
rs.getString("title"),
rs.getString("content").replace("\n", "<br>"),
rs.getString("pwd"),
rs.getString("wdate").substring(0,10),
rs.getInt("hit")
);
boardList.add(BoardDto);
}
}catch(Exception e){}
finally{
rs.close();
stat.close();
}
return boardList;
}
}
-------------------------------------------list.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="util.*" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!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=EUC-KR">
<title>글목록</title>
<script type="text/javascript">
<!--
function send(f){
if(f.search.value==""){
alert("검색어 입력!!");
f.search.focus();
return false;
}
f.submit();
}
//-->
</script>
</head>
<body>
<br/>* 글목록<br/><br/>
글갯수 : <bean:write name="rcnt"/>
<br/>
<table border=1>
<tr>
<td>번호</td>
<td>일자</td>
<td>제목</td>
<td>이름</td>
<td>조회</td>
</tr>
<logic:notEmpty name="list">
<logic:iterate id="boardList" name="list">
<tr>
<td><bean:write name="boardList" property="idx"/></td>
<td><bean:write name="boardList" property="wdate"/></td>
<td><a href="Content.do?idx=<bean:write name="boardList" property="idx"/>"><bean:write name="boardList" property="title"/></a></td>
<td><bean:write name="boardList" property="name"/></td>
<td><bean:write name="boardList" property="hit"/></td>
</tr>
</logic:iterate>
</logic:notEmpty>
</table>
<br/>
<%
int pagelink=1;
String pagelink_get=request.getParameter("pagelink");
if(pagelink_get==null){
pagelink=1;
}
else{
pagelink=Integer.parseInt(pagelink_get);
}
Integer rcnt=(Integer)request.getAttribute("rcnt");
PagingCount pc=new PagingCount(rcnt.intValue());
out.println(pc.showPaging(pagelink, "List.do"));
%>
<br/>
<%--
<html:form method="post" action="Search.do" focus="search">
<table border=1>
<tr>
<td>
<html:select property="find">
<html:option value="name">이름</html:option>
<html:option value="title">제목</html:option>
<html:option value="content">내용</html:option>
</html:select>
<html:text property="search"></html:text>
<html:button property="btn" onclick="send(this.form)">검색</html:button>
</td>
</tr>
</table>
</html:form>
--%>
<br/>
<a href="Write.do">글쓰기</a>
</body>
</html>
-------------------------------------------PagingCount.java
package util;
public class PagingCount {
int links=10 ;
int limit=10;
int totalpage = 0 ;
public PagingCount() {
super();
}
public PagingCount(int numrows) {
super();
this.totalpage=((numrows-1)/limit)+1 ;
}
public PagingCount(int numrows, int links, int limit) {
super();
this.links = links;
this.limit = limit;
this.totalpage=((numrows-1)/limit)+1 ;
}
public int countOffset (int page) {
int offset=(page-1) * this.limit;
return offset ;
}
public String showPaging(int pagelink,String pagename) {
String str = "";
str="<TABLE width=\"550\" border=\"1\" align=\"center\">";
str=str+"<TR><TD align=\"center\" bgcolor=\"#CCCCCC\">";
int bpage = 0 ;
int boffset = 0 ;
int cpage = 0 ;
int coffset = 0 ;
int vpage = 0 ;
int noffset = 0 ;
int loffset = 0 ;
// <!-- 1 페이지 -->
if(pagelink != 1 ){
str=str+"[<A HREF="+pagename+"?pagelink=1&offset=0><FONT SIZE=2 COLOR=red>처음</FONT></A>]";
}
else{
str=str+"[<FONT SIZE=2 COLOR=gray>처음</FONT>]";
}
// <!-- 이전10 -->
bpage=pagelink - 10 ;
boffset= countOffset (bpage);
if(pagelink > links){
str = str + "[<A HREF="+pagename+"?pagelink="+bpage+"&offset="+boffset+"><FONT SIZE=2 COLOR=blue>이전10</FONT></A>]" ;
}
else{
str = str + "[<FONT SIZE=2 COLOR=gray>이전10</FONT>]" ;
}
// <!-- 링크페이지 -->
vpage = pagelink ; // 현재페이지를 잠깐 다른변수에 아래서 사용함
pagelink= ((pagelink-1)/links)*links+1 ; // 시작 페이지 구하기
for(cpage=pagelink; cpage < pagelink+links; cpage++) {
if(cpage>totalpage){
break ;
}
coffset=countOffset (cpage);
if(cpage!=vpage){
str = str + "[<A HREF="+pagename+"?pagelink="+cpage+"&offset="+coffset+"><FONT SIZE=2 COLOR=black>"+cpage+"</FONT></A>]" ;
}
else{
str = str + "[<FONT SIZE=2 COLOR=red>"+cpage+"</FONT>]" ;
}
}
// <!-- 다음10 -->
// for문을 빠져 나왔을때 cpage의 값은 1만큼 더 증가되어있기 때문에 그냥 사용한다.
noffset=countOffset (cpage);
if((totalpage-pagelink) >= links){
str = str + "[<A HREF="+pagename+"?pagelink="+cpage+"&offset="+noffset+"><FONT SIZE=2 COLOR=blue>다음10</FONT></A>]" ;
}
else{
str = str + "[<FONT SIZE=2 COLOR=gray>다음10</FONT>]" ;
}
// <!-- 마지막페이지 -->
loffset=countOffset (totalpage);
if(vpage!=totalpage){
str = str + "[<A HREF="+pagename+"?pagelink="+totalpage+"&offset="+loffset+"><FONT SIZE=2 COLOR=red>마지막</FONT></A>]" ;
}
else{
str = str + "[<FONT SIZE=2 COLOR=gray>마지막</FONT>]" ;
}
str = str + "</TD></TR></TABLE>" ;
return str ;
}
public String showPaging(int pagelink,String pagename,String find,String search) {
String str = "";
str="<TABLE width=\"550\" border=\"1\" align=\"center\">";
str=str+"<TR><TD align=\"center\" bgcolor=\"#CCCCCC\">";
int bpage = 0 ;
int boffset = 0 ;
int cpage = 0 ;
int coffset = 0 ;
int vpage = 0 ;
int noffset = 0 ;
int loffset = 0 ;
// <!-- 1 페이지 -->
if(pagelink != 1 ){
str=str+"[<A HREF="+pagename+"?pagelink=1&offset=0&find="+find+"&search="+search+"><FONT SIZE=2 COLOR=red>처음</FONT></A>]";
}
else{
str=str+"[<FONT SIZE=2 COLOR=gray>처음</FONT>]";
}
// <!-- 이전10 -->
bpage=pagelink - 10 ;
boffset= countOffset (bpage);
if(pagelink > links){
str = str + "[<A HREF="+pagename+"?pagelink="+bpage+"&offset="+boffset+"&find="+find+"&search="+search+"><FONT SIZE=2 COLOR=blue>이전10</FONT></A>]" ;
}
else{
str = str + "[<FONT SIZE=2 COLOR=gray>이전10</FONT>]" ;
}
// <!-- 링크페이지 -->
vpage = pagelink ; // 현재페이지를 잠깐 다른변수에 아래서 사용함
pagelink= ((pagelink-1)/links)*links+1 ; // 시작 페이지 구하기
for(cpage=pagelink; cpage < pagelink+links; cpage++) {
if(cpage>totalpage){
break ;
}
coffset=countOffset (cpage);
if(cpage!=vpage){
str = str + "[<A HREF="+pagename+"?pagelink="+cpage+"&offset="+coffset+"&find="+find+"&search="+search+"><FONT SIZE=2 COLOR=black>"+cpage+"</FONT></A>]" ;
}
else{
str = str + "[<FONT SIZE=2 COLOR=red>"+cpage+"</FONT>]" ;
}
}
// <!-- 다음10 -->
// for문을 빠져 나왔을때 cpage의 값은 1만큼 더 증가되어있기 때문에 그냥 사용한다.
noffset=countOffset (cpage);
if((totalpage-pagelink) >= links){
str = str + "[<A HREF="+pagename+"?pagelink="+cpage+"&offset="+noffset+"&find="+find+"&search="+search+"><FONT SIZE=2 COLOR=blue>다음10</FONT></A>]" ;
}
else{
str = str + "[<FONT SIZE=2 COLOR=gray>다음10</FONT>]" ;
}
// <!-- 마지막페이지 -->
loffset=countOffset (totalpage);
if(vpage!=totalpage){
str = str + "[<A HREF="+pagename+"?pagelink="+totalpage+"&offset="+loffset+"&find="+find+"&search="+search+"><FONT SIZE=2 COLOR=red>마지막</FONT></A>]" ;
}
else{
str = str + "[<FONT SIZE=2 COLOR=gray>마지막</FONT>]" ;
}
str = str + "</TD></TR></TABLE>" ;
return str ;
}
}
-----------------------------------------------
'..열심히 공부하세.. > MVC' 카테고리의 다른 글
[17] Aspectj의 설치 및 셋팅 - ② (0) | 2012.07.11 |
---|---|
[16] Spring Framework 개요 - ① (0) | 2012.07.11 |
[14] Struts1 기반 게시판 쓰기 - ① (0) | 2012.07.04 |
[13] Struts1 기본문법 (0) | 2012.07.03 |
[12] MVC2패턴 게시판 - ⑥ 삭제 (0) | 2012.06.30 |