YSHUSH

게시판2. 게시판 목록 & 글쓰기 본문

Coding/SpringFramework

게시판2. 게시판 목록 & 글쓰기

코딩; 2022. 1. 25. 08:06

 

1. BbsController

@Controller
public class BbsController {

	private static Logger logger = LoggerFactory.getLogger(BbsController.class);
	
	@Autowired
	BbsService service;
	
	@RequestMapping(value = "bbslist.do", method = RequestMethod.GET)
	public String bbslist(Model model) {
		logger.info("BbsController bbslist() " + new Date());
	
		return "bbslist";
	}
}

짐(데이터)를 들고 가기 위해서는 model이 필요하다!

 

2. Bbs.xml

<mapper namespace="Bbs">

<select id="bbslist" parameterType="mul.camp.a.dto.BbsParam" resultType="mul.camp.a.dto.BbsDto">
	SELECT SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT
	FROM BBS
</select>

</mapper>

 

3. BbsDao, BbsDaoImpl

BbsDao

public interface BbsDao {

	List<BbsDto> bbslist();
    
}

BbsDaoImpl

@Repository
public class BbsDaoImpl implements BbsDao{

	@Autowired
	SqlSession session;
	
	String ns = "Bbs.";

	@Override
	public List<BbsDto> bbslist() {		
		return session.selectList(ns + "bbslist");
	}
}

 

4. BbsService, BbsServiceImpl

BbsService

public interface BbsService {

	List<BbsDto> bbslist();

}

BbsServiceImpl

@Service
public class BbsServiceImpl implements BbsService{

	@Autowired
	BbsDao dao;

	@Override
	public List<BbsDto> bbslist(BbsParam param) {		
		return dao.bbslist(param);
        
	}
}

 

5. BbsController - bbslist에 짐쌀 list 추가

	@RequestMapping(value = "bbslist.do", method = RequestMethod.GET)
	public String bbslist(Model model) {
		logger.info("BbsController bbslist() " + new Date());
		
		List<BbsDto> list = service.bbslist();
		model.addAttribute("bbslist", list);
		
		return "bbslist";
	}

 

6. views에 bbslist생성 후 형식 업데이트

controller에서 가져온 짐(list)을 풀기 위해서 상단부에

<%List<BbsDto> bbslist = (List<BbsDto>)request.getAttribute("bbslist");%> 입력

<%
List<BbsDto> bbslist = (List<BbsDto>)request.getAttribute("bbslist");
%>    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>

<h1>게시판</h1>
<div align="center">
    <thead>
    <tr>
        <th>번호</th><th>제목</th><th>정보</th><th>작성자</th>
    </tr>
    </thead>
    <table border="1" style="width: 1000px">		// 임시 스타일
    <col width="30"><col width="200"><col width="80">	// 임시 스타일
    <tbody>
    <%
    if(bbslist == null || bbslist.size() == 0){
        %>
        <tr>
            <td colspan="3">작성된 글이 없습니다</td> 
        </tr>
        <%
    }else{
        for(int i = 0;i < bbslist.size(); i++){
            BbsDto bbs = bbslist.get(i);
        %>
            <tr>	
                <th><%=i+1 %></th>
                <td>
                    <a href="bbsdetail.do?seq=<%=bbs.getSeq() %>">
                        <%=bbs.getTitle() %>
                    </a>
                </td>
                <td><%=bbs.getId() %></td>
            </tr>
        <%
        }
    }
    %>
    </tbody>
    </table>
    </div>
    <br>
    <div align="center">
        <a href="bbswrite.do">글쓰기</a>
    </div>  
</body>
</html>

 

글쓰기

6. BbsController 업데이트

	@RequestMapping(value = "bbswrite.do", method = RequestMethod.GET)
	public String bbswrite() {
		logger.info("BbsController bbswrite() " + new Date());
		
		return "bbswrite";
	}

 

7. views에 bbwrite생성 후 형식 업데이트

<%
// session에서 사용자 정보를 산출 (로그인으로부터) -> 글을 추가하려면 작성자가 있어야 하기 때문에
MemberDto mem = (MemberDto)request.getSession().getAttribute("login");
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>글추가</h1>

<div align="center">

<form action="bbswriteAf.do" method="post">

<table border="1" style="width: 1000px">
<tr>
	<th>아이디</th>
	<td>
		<input type="text" name="id" size="70px" value="<%=mem.getId() %>" readonly="readonly">
	</td>	
</tr>
<tr>
	<th>제목</th>
	<td>
		<input type="text" name="title" size="70px">
	</td>
</tr>
<tr>
	<th>내용</th>
	<td>
		<textarea rows="20" cols="80" name="content"></textarea>
	</td>
</tr>
<tr>
	<td colspan="2">
		<input type="submit" value="글쓰기">
	</td>
</tr>
</table>
</form>
</div>
</body>
</html>

 

 8. Bbs.xml

<insert id="writebbs" parameterType="mul.camp.a.dto.BbsDto">
	INSERT INTO BBS(SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT)
	VALUES(SEQ_BBS.NEXTVAL, #{id}, (SELECT NVL(MAX(REF)+1, 0) FROM BBS), 0, 0,       
											#{title}, #{content}, SYSDATE, 0, 0)
</insert>

 

 

9. BbsController 업데이트

	@RequestMapping(value = "bbswriteAf.do", method = RequestMethod.POST)
	public String bbswriteAf(BbsDto dto) {
		logger.info("BbsController bbswriteAf() " + new Date());
		System.out.println(dto.toString());		// 확인

		return "redirect:/bbslist.do";
	}

 

10. BbsDao, BbsDaoImpl

BbsDao

int writebbs(BbsDto dto);

 

BbsDaoImpl

@Override
public int writebbs(BbsDto dto) {
    int count = session.insert(ns + "writebbs", dto);
    return count;
}

 

 

11. BbsService, BbsServiceImpl

BbsService

boolean writebbs(BbsDto dto);

BbsServiceImpl

@Override
public boolean writebbs(BbsDto dto) {
    int count = dao.writebbs(dto);
    return count>0?true:false;
}

 

12. BbsController 업데이트 (bbswriteAf)

@RequestMapping(value = "bbswriteAf.do", method = RequestMethod.POST)
public String bbswriteAf(BbsDto dto) {
    logger.info("BbsController bbswriteAf() " + new Date());
    System.out.println(dto.toString());		// 확인
	
    // DB에 넣어주는 부분
    boolean b = service.writebbs(dto);
    if(b == true) {
        System.out.println("성공적으로 추가되었습니다");
    }

    return "redirect:/bbslist.do";
}