STUDY/STRUTS

Struct1 + iBatis 게시판(3)

Anne of Green Galbes 2019. 3. 20. 19:39

4. article 작성(게시글 보기)


4-1 boardTest_sqlMpa.xml 작성

○ 이전글 / 다음글 기능

- 클릭한 번호에서 이전글은 클릭한 번호보다 큰 데이터를 ASC로 정렬

다음글은 클릭한 번호보다 작은 데이터를  DESC로 정렬 후, 맨 처음 하나를 가져온다

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">


<sqlMap namespace="boardTest">


<!-- 게시물 하나의 데이터 -->

<select id="readData" resultClass="com.boardTest.BoardForm" parameterClass="int">

select num,name,subject,email,pwd,content,ipAddr,hitCount,created

from board where num=#num#

</select>


<!-- 이전글 : 검색을 했다면 searchKey / searchValue값을 가지고 가야한다. -->

<select id="preReadData" resultClass="com.boardTest.BoardForm" parameterClass="map">

select data.* from (

select num,subject from board

where ($searchKey$ like '%' || #searchValue# || '%')

and (num>#num#) order by num asc) data

where rownum=1

</select>


<!-- 다음글 : 검색을 했다면 searchKey / searchValue값을 가지고 가야한다. -->

<select id="nextReadData" resultClass="com.boardTest.BoardForm" parameterClass="map">

<![CDATA[

select data.* from (

select num,subject from board

where ($searchKey$ like '%' || #searchValue# || '%')

and (num<#num#) order by num desc) data

where rownum=1

]]>

</select>


<!-- 조회수 증가 -->

<update id="hitCountUpdate" parameterClass="int">

update board set hitCount=hitCount+1

where num = #num#

</update>


</sqlMap>



4-2 BoardAction.java 작성

public ActionForward article(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws Exception {

CommonDAO dao = CommonDAOImpl.getInstance();

String cp = request.getContextPath();

int num = Integer.parseInt(request.getParameter("num"));

String pageNum = request.getParameter("pageNum");

String searchKey = request.getParameter("searchKey");

String searchValue = request.getParameter("searchValue");

if(searchValue==null){

searchKey = "subject";

searchValue = "";

}

if(request.getMethod().equalsIgnoreCase("GET"))

searchKey = URLDecoder.decode(searchKey,"UTF-8");

//조회수 증가

dao.updateData("boardTest.hitCountUpdate", num);

//하나의 데이터 가져오기

//반환형태가 object이므로 다운캐스팅 해줘야한다.

BoardForm dto = (BoardForm)dao.getReadData("boardTest.readData", num);

//dto의 값이 없으면 list화면을 보여준다

if(dto==null)

return mapping.findForward("list");

int lineSu = dto.getContent().split("\n").length;

dto.setContent(dto.getContent().replaceAll("\n", "<br/>"));

//이전글 다음글

String preUrl = "";

String nextUrl = "";

//boardTest_sqlMpa.xml에서 매개변수를 map으로 받아, map 생성

Map<String, Object> hMap = new HashMap<String, Object>();

hMap.put("searchKey", searchKey);

hMap.put("searchValue", searchValue);

hMap.put("num", num);

String preSubject = "";


//이전 게시물 정보가져온다

BoardForm preDTO = (BoardForm)dao.getReadData("boardTest.preReadData", hMap);

if(preDTO!=null){

preUrl = cp + "/boardTest.do?method=article&pageNum=" +pageNum + "&num=" + preDTO.getNum();

preSubject = preDTO.getSubject();

}

String nextSubject = "";


//다음 게시물 정보

BoardForm nextDTO = (BoardForm)dao.getReadData("boardTest.nextReadData", hMap);

if(nextDTO!=null){

nextUrl = cp + "/boardTest.do?method=article&pageNum=" +pageNum + "&num=" + nextDTO.getNum();

nextSubject = nextDTO.getSubject();

}

String urlList = cp + "/boardTest.do?method=list&pageNum=" + pageNum;

//수정과 삭제에서 사용할 인수

String paramArticle = "num=" + num + "&pageNum=" + pageNum;

if(!searchValue.equals("")){

searchValue = URLEncoder.encode(searchValue,"UTF-8");

//나중에 article에서 리스트로 돌아갈 때 사용

urlList += "&searchKey=" + searchKey + "&searchValue=" + searchValue;

if(!preUrl.equals("")){

preUrl += "&searchKey=" + searchKey + "&searchValue=" + searchValue;

}

if(!nextUrl.equals("")){

nextUrl += "&searchKey=" + searchKey + "&searchValue=" + searchValue;

}

paramArticle+= "&searchKey=" + searchKey + "&searchValue=" + searchValue;;

}

request.setAttribute("dto", dto);

request.setAttribute("preSubject", preSubject);

request.setAttribute("preUrl", preUrl);

request.setAttribute("nextSubject", nextSubject);

request.setAttribute("nextUrl", nextUrl);

request.setAttribute("lineSu", lineSu);

request.setAttribute("paramArticle", paramArticle);

request.setAttribute("urlList", urlList);

return mapping.findForward("article");

}



4-3 struts-config_boardTest.xml 작성

○ 메핑 포워드로 article값이 오면 /boardTest/article.jsp파일을 실행

<action-mappings>

<action path="/boardTest" type="com.boardTest.BoardAction" name="boardTestForm" scope="request" parameter="method">

<forward name="created" path="/boardTest/created.jsp"/>

<forward name="created_ok" redirect="true" path="/boardTest.do?method=list"/>

<forward name="list" path="/boardTest/list.jsp"/>

<forward name="article" path="/boardTest/article.jsp"/>

</action>

</action-mappings>


4-4 article.jsp 작성

○ 현재 num이 15, pageNum이 2일 때,

   - preUrl : cp + "/boardTest.do?method=article&pageNum=2&num=16";

   - nextUrl : cp + "/boardTest.do?method=article&pageNum=2&num=14"; 

   - urlList : cp + "/boardTest.do?method=list&pageNum=2";

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

%>


<!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>게 시 판(Struts1 + iBatis)</title>

<link rel="stylesheet" href="<%=cp %>/board/css/style.css" type="text/css" />

<link rel="stylesheet" href="<%=cp %>/board/css/article.css" type="text/css" />

</head>


<body>


<div id="bbs">

<div id="bbs_title">

게 시 판(Struts1 + iBatis)

</div>

<div id="bbsArticle">

<div id="bbsArticle_header">

${dto.subject }

</div>

<div class="bbsArticle_bottomLine">

<dl>

<dt>작성자</dt>

<dd>${dto.name }</dd>

<dt>줄수</dt>

<dd>${lineSu }</dd>

</dl>

</div>

<div class="bbsArticle_bottomLine">

<dl>

<dt>등록일</dt>

<dd>${dto.created }</dd>

<dt>조회수</dt>

<dd>${dto.hitCount }</dd>

</dl>

</div>

<div id="bbsArticle_content">

<table width="600" border="0">

<tr>

<td style="padding: 20px 80px 20px 62px;" valign="top" height="200">

${dto.content }

</td>

</tr>

</table>

</div>

<div class="bbsArticle_bottomLine">

이전글:<c:if test="${!empty preUrl}"><a href="${preUrl }">${preSubject }</a></c:if>

</div>

<div class="bbsArticle_noLine">

다음글:<c:if test="${!empty nextUrl}"><a href="${nextUrl }">${nextSubject }</a></c:if>

</div>

</div>

<div class="bbsArticle_noLine" style="text-align: right;">

from ${dto.ipAddr }

</div>

<div id="bbsArticle_footer">

<div id="leftFooter">

<input type="button" value=" 수정 " class="btn2" onclick="" />

<input type="button" value=" 삭제 " class="btn2" onclick="" />

</div>


<div id="rightFooter">

<input type="button" value=" 리스트 " class="btn2"

onclick="javascript:location.href='${urlList }';" />

</div>

</div>

</div>


</body>

</html>


4-5 실행화면




4-6 실행흐름

① list화면에서 게시물 클릭

  ○ method = article

  ○ 같이 넘어가는 값 : pageNum / num, 검색을 했을 경우 searchKey / searchValue

    - pagaNum = 4

    - num = 17 

    - 검색을 안 했기 때문에 searchKey, searchValue값은 넘어가지 않는다


② struts-config_boardTest.xml

○ /boardTest.do가 오며 BoardAction.java로 이동


③ BoardAction에서 list메소드 실행 

 ○ 메핑 포워드 값 : article → struts-config_boardTest.xml에서 포워드 이름이 article인 곳을 찾아감


④ <forward name="article" path="/boardTest/article.jsp"/>에 의해 article.jsp 실행


⑤ 이전글 / 다음글 클릭시 위 상황 반복


⑥ 리스트 클릭시 

  ○ metho = list 

  ○ 같이 넘어가는 값 : pageNum, 검색시 searchKey / searchValue