게시판 만들기 (게시글 목록)

Back-End/Spring 2019. 7. 2. 16:20
728x90
반응형

- 게시글 목록 보기 구현 -



1. menu.jsp에 게시글 목록을 볼수있는 링크를 추가

1
<a href="${path}/board/list.do">게시판</a> |  
cs



2. BoardDTO.java 클래스 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.example.spring02.model.board.dto;
 
import java.util.Arrays;
import java.util.Date;
 
//게시글을 저장할 dto 클래스
public class BoardDTO {
    private int bno;        //게시물 번호
    private String title;    //제목
    private String content;    //내용
    private String writer;     //작성자 아이디
    private Date regdate;     //날짜
    private int viewcnt;    //조회수
    private String name;     //작성자 이름 (member 테이블과 조인할것을 고려해서 만들었음)
    private int cnt;         //댓글 갯수
    private String show;     //화면 표시 여부
    private String[] files; //첨부파일 배열
    
    //getter,setter,toString()
    
    public int getBno() {
        return bno;
    }
    public void setBno(int bno) {
        this.bno = bno;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getWriter() {
        return writer;
    }
    public void setWriter(String writer) {
        this.writer = writer;
    }
    public Date getRegdate() {
        return regdate;
    }
    public void setRegdate(Date regdate) {
        this.regdate = regdate;
    }
    public int getViewcnt() {
        return viewcnt;
    }
    public void setViewcnt(int viewcnt) {
        this.viewcnt = viewcnt;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getCnt() {
        return cnt;
    }
    public void setCnt(int cnt) {
        this.cnt = cnt;
    }
    public String getShow() {
        return show;
    }
    public void setShow(String show) {
        this.show = show;
    }
    public String[] getFiles() {
        return files;
    }
    public void setFiles(String[] files) {
        this.files = files;
    }
    @Override
    public String toString() {
        return "BoardDTO [bno=" + bno + ", title=" + title + ", content=" + content + ", writer=" + writer + ", regdate="
                + regdate + ", viewcnt=" + viewcnt + ", name=" + name + ", cnt=" + cnt + ", show=" + show + ", files="
                + Arrays.toString(files) + "]";
    }
    
}
 
cs



3. BoardDAO.java 만들기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.example.spring02.model.board.dao;
 
import java.util.List;
 
import com.example.spring02.model.board.dto.BoardDTO;
 
public interface BoardDAO {
    public void deleteFile(String fullName); //첨부파일 삭제
    public List<String> getAttach(int bno); //첨부파일 정보
    public void addAttach(String fullName); //첨부파일 저장
    
    
    public void updateAttach(String fullName, int bno); //첨부파일 수정
    public void create(BoardDTO dto) throws Exception; //글쓰기 
    public void update(BoardDTO dto) throws Exception; //글수정
    public void delete(int bno) throws Exception; //글삭제
    
    //목록 (페이지 나누기, 검색 기능 포함)
    //매개변수는 시작 레코드번호, 끝번호, 옵션과 키워드가 들어간다)

    public List<BoardDTO> listAll(
String search_option, String keyword,int start, int end) 
                throws Exception;
    
    //조회수 증가 처리
    public void increateViewcnt(int bno) throws Exception;
    
    //레코드 갯수 계산
    public int countArticle(
            String search_option, String keyword) throws Exception;
    
    //레코드 조회
    public BoardDTO read(int bno) throws Exception;
}
cs



4. boardDAOImpl.java (인터페이스 구현 클래스) 중 일부

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.example.spring02.model.board.dao;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.inject.Inject;
 
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
 
import com.example.spring02.model.board.dto.BoardDTO;
 
@Repository // dao bean
public class BoardDAOImpl implements BoardDAO {
 
    @Inject //의존관계 주입(Dependency Injection, DI)
    SqlSession sqlSession;
 
//게시물 목록 리턴
    @Override
    public List<BoardDTO> listAll(
//매개변수는 시작 레코드번호, 끝번호, 옵션과 키워드가 들어간다)
String search_option, String keyword,int start, int end) 
                throws Exception {     
        return sqlSession.selectList("board.listAll"); 
    }
cs



5. boardMapper.xml 만들기

1
2
3
4
5
6
7
    <!-- 다른 mapper와 중복되지 않도록 네임스페이스 기재 --> <mapper namespace="board">

<select id="listAll"
    resultType="com.example.spring02.model.board.dto.BoardDTO">
    <!-- 결과는 boardDTO타입이 된다. -->
            select bno,title,writer,name,regdate,viewcnt                    
            from board
            order by bno desc   
    </select>
cs



6. boardService.java 만들기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.example.spring02.service.board;
 
import java.util.List;
 
import javax.servlet.http.HttpSession;
 
import com.example.spring02.model.board.dto.BoardDTO;
 
public interface BoardService {
    public void deleteFile(String fullName);    //첨부파일 삭제
    public List<String> getAttach(int bno);        //첨부파일 목록
    public void create(BoardDTO dto) throws Exception;    //글쓰기
    public BoardDTO read(int bno) throws Exception;        //글읽기
    public void update(BoardDTO dto) throws Exception;    //글 수정
    public void delete(int bno) throws Exception;        //글 삭제
    
 
    //목록 (페이지 나누기, 검색 기능 포함)
    //매개변수는 시작 레코드번호, 끝번호, 옵션과 키워드가 들어간다)
    public List<BoardDTO> listAll(
String search_option, String keyword,int start, int end) 
                throws Exception;
    
    //조회수 증가 처리
    public void increaseViewcnt(int bno, HttpSession session) throws Exception; 
    
    //레코드 갯수 계산
    public int countArticle( String search_option, String keyword) throws Exception;
}
cs



7. boardServiceImpl.java 만들기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.example.spring02.service.board;
 
import java.util.List;
 
import javax.inject.Inject;
import javax.servlet.http.HttpSession;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.example.spring02.model.board.dao.BoardDAO;
import com.example.spring02.model.board.dto.BoardDTO;
 
@Service // service bean
public class BoardServiceImpl implements BoardService {
 
    @Inject //dao를 호출하기 때문에 의존성을 주입
    BoardDAO boardDao; 
 
 
@Override
    public List<BoardDTO> listAll(
    String search_option, String keyword,int start, int end)
            throws Exception {
        return boardDao.listAll(search_option,keyword,start,end); 
    }
cs



8. BoardController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.example.spring02.controller.board;
 
import java.util.HashMap;
import java.util.List;
 
import javax.inject.Inject;
import javax.servlet.http.HttpSession;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 
import com.example.spring02.model.board.dto.BoardDTO;
import com.example.spring02.service.board.BoardService;
import com.example.spring02.service.board.Pager;
 
@Controller //controller bean
@RequestMapping("board/*"//공통적인 url pattern
 
public class BoardController {
    
    @Inject 
    BoardService boardService; //서비스를 사용하기 위해 의존성을 주입
 
@RequestMapping("list.do"//세부적인 url pattern
    public ModelAndView list() throws Exception{
        List<BoardDTO> list=
boardService.listAll(search_option,keyword,start,end); //게시물 목록
        ModelAndView mav=new ModelAndView();
        Map<String,Object> map=new HashMap<>(); //데이터가 많기 때문에 hashmap<>에 저장한다.
        map.put("list", list); //map에 list(게시글 목록)을 list라는 이름의 변수로 자료 저장
        mav.addObject("map", map); //ModelAndView에 map을 저장
        return mav; // board/list.jsp로 이동
    }
    
cs



9. list.jsp (view 파일)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%@ include file="../include/header.jsp" %>
</head>
<body>
<%@ include file="../include/menu.jsp" %>
<h2>게시판</h2>
<table border="1" width="600px">
    <tr>
        <th>번호</th>
        <th>제목</th>
        <th>이름</th>
        <th>날짜</th>
        <th>조회수</th>
    </tr>
    <!-- forEach var="개별데이터" items="집합데이터" -->
<c:forEach var="row" items="${map.list}"<!-- 컨트롤러에서 map안에 list를 넣었기 때문에 이렇게 받는다. -->
    <tr>
        <td>${row.bno}</td> //글 번호
        <td>${row.title}</td> //글 제목
        <td>${row.writer}</td> // 작성자 이름
        <td><fmt:formatDate value = "{row.regdate}"
            pattern="yyyy-MM-dd HH:mm:ss"    /></td> //날짜의 출력형식을 변경
        <td>${row.viewcnt}</td> //조회수
    </tr>
</c:forEach>
</table>
</body>
</html>
cs




728x90
반응형
: