본문 바로가기

Project/2. My First Website <Mybatis>

project 3.1 My first website/ 게시판 리스트

결과 창 

 

게시글을  보는건 비회원도 가능하나, 글 작성은 회원만 가능하다.

 

1. new/ my post 버튼은 로그인유저정보가 세션에 있어야만 나타남

2. common 패키지에 단순 페이지 이동인 ViewForward 와 게시글페이지를 나타내는 Paging 클래스가 있다.

3. 보드 리스트에는 페이징도있고.. c:if도 있어서 html을 넣었다.

c:if / c:forEach 등 jstl을 사용하기 위해 xml 에 라이브러리를 넣어줬고

jsp에서도 상단에 태그립을 작성해줌

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

1. view (boardList.jsp)

<script>

// 검색 결과 목록 보기

function fn_queryBoardList(f) {

f.action = '/MYHOME_P/queryBoardList.board';

f.submit();

}

// 내 게시글 목록 보기

function fn_myBoardList(f) {

f.action = '/MYHOME_P/myBoardList.board';

f.submit();

}

// 새 게시글 등록

function fn_boardInsertPage(f) {

f.action = '/MYHOME_P/boardInsertPage.board';

f.submit();

}

// 전체 게시글 목록 보기

function fn_boardList(f) {

f.action = '/MYHOME_P/boardList.board';

f.submit();

}

</script>

 

<!--html-->

 

<div class="boardList">

Total: &nbsp;  ${totalRecord} posts <br/>

</div>

<hr>

<!-- 게시판 검색 -->

 

<div class="search">

<form>

<select name="column" id="searchSelect">

<option value="BTITLE">Title</option>

<option value="BCONTENT">Content</option>

<option value="BOTH">Title+Content</option>

</select>

<input type="text" name="query" />

<input type="button" value="Search" onclick="fn_queryBoardList(this.form)" />

</form>

</div>

<br/><br/>

 

<table>

<thead>

<tr>

<th>title</th>

<th>writer</th>

<th>date</th>

<th>views</th>

</tr>

</thead>

<tbody>

<c:if test="${empty list}">

<tr>

<td colspan="4">There is no post yet.</td>

</tr>

</c:if>

<c:if test="${not empty list}">

<c:forEach var="bDto" items="${list}">

<tr>

<td>

 

<!-- 1. 댓글은 bDepth 만큼 들여쓰기를 한다. -->

<c:forEach begin="1" end="${bDto.bDepth}" step="1">

&nbsp;&nbsp;&nbsp;

</c:forEach>

 

<!-- 2. 댓글은 제목 앞에 ㄴRe. 를 표시한다. -->

<c:if test="${bDto.bDepth ne 0}">

ㄴRe.

</c:if>

 

<!-- 3. 삭제된 게시글은 링크를 제공하지 않는다. -->

<c:if test="${bDto.bDelete eq 0}">

<a href="/MYHOME_P/boardView.board?bNo=${bDto.bNo}&page=${page}">${bDto.bTitle}</a>

</c:if>

<c:if test="${bDto.bDelete eq -1}">

${bDto.bTitle}(This post has been deleted.)

</c:if>

 

 

</td>

<td>${bDto.mId}</td>

<td>${bDto.bRegDate}</td>

<td>${bDto.bHit}</td>

</tr>

</c:forEach>

</c:if>

</tbody>

</table>

<br/><br/><br/>

<div class="page">

<tr>

<td colspan="4" style='margin: auto;'>${paging}</td>

</tr>

</div>

 

<br/>

<div class="newPost">

<form>

<!-- 게시판 작성 버튼 : 로그인해야 작성할 수 있다. -->

<input type="hidden" name="page" value="${page}" />

<!--  <input type="button" value="List" onclick="fn_boardList(this.form)" />-->

<c:if test="${loginUser ne null}">

<button type="button" class="btn btn-outline-warning" onclick="fn_boardInsertPage(this.form)" >NEW</button>

<!--  <input type="button" value="NEW"  />--> &nbsp;

<button type="button" class="btn btn-outline-info" onclick="fn_myBoardList(this.form)">MY POST</button>

<!--<input type="button" value="MY POST"  />-->

<input type="hidden" name="mId" value="${loginUser.mId}" />

</c:if>

</form>

</div>

 

 

2. Paging command

 

3. Controller

검색 결과에 따라 게시판 글들을 나열해주는 쿼리보드리스트와 게시판글들을 나열해주는 보드리스트가 있음

 

case "/boardList.board":

boardCommand = new BoardListCommand();

vf = boardCommand.excute(request, response);

break;

case "/queryBoardList.board" :

boardCommand = new QueryBoardListCommand();

vf = boardCommand.excute(request, response);

break;

 

 

4. Command

기본이 되는 boardCommand interface

public interface BoardCommand {

public ViewForward excute(HttpServletRequest request, HttpServletResponse response) throws Exception;

}

 

1) BoardListCommand

 

2)QueryBoardListCommand

 

 

5. Dao

// access to DB (리스트보여주기)

public List<BoardDto> selectBoardList(Map<String, Integer> map) {

SqlSession ss = factory.openSession();

List<BoardDto> list = ss.selectList("mybatis.mapper.board.selectBoardList", map);

ss.close();

return list;

}

 

// xml->Dao (총 게시물 개수 보여주기)

public int selectBoardCount() {

SqlSession ss = factory.openSession();

int totalRecord = ss.selectOne("mybatis.mapper.board.selectBoardCount");

ss.close();

return totalRecord;

}

 

// 검색 결과 리스트 보여주기

public List<BoardDto> selectQueryBoardList(Map<String, String> map) {

SqlSession ss = factory.openSession();

List<BoardDto> list = ss.selectList("mybatis.mapper.board.selectQueryBoardList",map);

ss.close();

return list;

}

 

// 검색된 결과 게시물 개수 보여주기 

public int selectQueryBoardCount(Map<String, String> map) {

SqlSession ss = factory.openSession();

int totalQueryRecord = ss.selectOne("mybatis.mapper.board.selectQueryBoardCount",map);

ss.close();

return totalQueryRecord;

}

 

 

6. mapper

1) boardList

<select id="selectBoardList" parameterType="Map" resultType="dto.BoardDto">

SELECT *

FROM (SELECT ROWNUM AS RN, A.*

      FROM (SELECT   *

            FROM     BOARD2

                ORDER BY BGROUP DESC, BDEPTH ASC, BGROUPORD ASC) A)

    WHERE RN BETWEEN #{begin} AND #{end}

</select>

 

2)boardCount

<select id="selectBoardCount" resultType="int">

SELECT COUNT(*)

  FROM BOARD2

</select>

 

3)queryBoardList

<select id="selectQueryBoardList" parameterType="Map" resultType="dto.BoardDto">

SELECT *

  FROM (SELECT ROWNUM AS RN, A.* 

    FROM (SELECT *

      FROM BOARD2

      <if test="column=='BTITLE'">WHERE BTITLE LIKE '%' || #{query} || '%'</if>

    <if test="column=='BCONTENT'">WHERE BCONTENT LIKE '%' || #{query} || '%'</if>

              <if test="column=='BOTH'">WHERE BTITLE LIKE '%' || #{query} || '%' OR BCONTENT LIKE '%' || #{query} || '%'</if>  

    ORDER BY BGROUP DESC, BDEPTH ASC, BGROUPORD ASC) A)

WHERE RN BETWEEN #{begin} AND #{end}  

</select>

 

4)queryBoardCount

<!-- number of searched results -->

<select id="selectQueryBoardCount" parameterType="Map" resultType="int">

SELECT COUNT(*)

  FROM BOARD2

  <if test="column=='BTITLE'">WHERE BTITLE LIKE '%' || #{query} || '%'</if>

  <if test="column=='BCONTENT'">WHERE BCONTENT LIKE '%' || #{query} || '%'</if>

  <if test="column=='BOTH'">WHERE BTITLE LIKE '%' || #{query} || '%' OR BCONTENT LIKE '%' || #{query} || '%'</if>

</select>