본문 바로가기

백/전자정부프레임워크

전자정부 프레임워크6 - 간단한 게시판 만들기(1)

db

CREATE TABLE NBOARD
(unq NUMBER PRIMARY KEY
,title VARCHAR2(100) NOT NULL
,pass VARCHAR2(100) NOT NULL
,name VARCHAR2(20) NOT NULL
,content VARCHAR2(4000) NOT NULL
,hits number
,rdate date
);

commit;

select * from nboard;


CREATE SEQUENCE NBOARD_SEQ
INCREMENT BY 1
START WITH 1
MAXVALUE 99999;

1. pom.xml

2.ojdbc

3.sql-map-config

=> 경로 sqlmap밑 바로 Board_SQL인걸로

4.context-datasource

5.dispatcher-servlet

 

경로주의

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="Board">

   <typeAlias  alias="egovMap" type="egovframework.rte.psl.dataaccess.util.EgovMap"/>
   <typeAlias  alias="BoardVO" type="main.service.BoardVO"/>


   <insert id="BoardDAO.insertBoard">
      <![CDATA[
         insert into nboard
			(
			unq
			,title
			,pass
			,name
			,content
			,hits
			,rdate
			)
			values
			(
			NBOARD_SEQ.nextval
			,#title#
			,#pass#
			,#name#
			,#content#
			,0
			,SYSDATE
			)
      ]]>
   </insert>

</sqlMap>

boardVO경로바꿔줌

main.service패키지 생성해서 vo넣기

boardVO

package main.service;

import java.sql.Timestamp;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class BoardVO {
	private int unq;
	private String title;
	private String pass;
	private String name;
	private String content;
	private int hits;
	private Timestamp rdate;	
}

 

package main.service.impl;

import org.springframework.stereotype.Repository;

import egovframework.rte.psl.dataaccess.EgovAbstractDAO;
import lombok.extern.slf4j.Slf4j;
import main.service.BoardVO;

@Repository("boardDAO")
@Slf4j
public class BoardDAO extends EgovAbstractDAO {
	
	public String insertNBoard(BoardVO vo) throws Exception {
		log.info("@# BoardDAO.insertNBoard()");
		return (String) insert("boardDAO.insertNBoard", vo);
	}

}

package main.service;

public interface BoardService {
//	접근 지정자 없으므로 default임
	String insertNBoard(BoardVO vo) throws Exception;
}

package main.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import egovframework.rte.fdl.cmmn.EgovAbstractServiceImpl;
import lombok.extern.slf4j.Slf4j;
import main.service.BoardService;
import main.service.BoardVO;


@Service("boardService")
@Slf4j
public class BoardServiceImpl extends EgovAbstractServiceImpl implements BoardService {

	@Resource(name = "boardDAO")
	private BoardDAO boardDAO;
	
	@Override
	public String insertNBoard(BoardVO vo) throws Exception {
		log.info("@#BoardServiceImpl insertNBoard()");
		return boardDAO.insertNBoard(vo);
	}

}

 

package main.web;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.slf4j.Slf4j;
import main.service.BoardService;
import main.service.BoardVO;

@Controller
@Slf4j
public class BoardController {
	
	@Resource(name = "boardService")
	private BoardService boardService;

	
	@RequestMapping(value = "/boardWrite.do")
	public String boardWrite() {
		return "board/boardWrite";
	}
	
	@ResponseBody
	@RequestMapping(value = "/boardWriteSave.do")
	public String boardWriteSave(BoardVO vo) throws Exception {
		
		String result = boardService.insertNBoard(vo);
		log.info("@# result ===>"+ result);
		
//		return "forward:/boardList.do";
		return "save";
	}
	
}

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
		table {
			width: 400px;
			border-collapse: collapse;
		}
		th td {
			border: 1px solid #cccccc;
			padding: 3px
		}
		.textarea{
			width: 98%;
			height: 70px;
		}
	</style>
	<script type="text/javascript">
// 		function fn_submit() {
// 			if(document.frm.title.value==""){
// 				alert("제목을 입력해주세요.");
// 				document.frm.title.focus();
// 				return false;
// 			}
// 			document.frm.submit();
// 		}
		function fn_submit() {
			if(!document.frm.checkValidity()){
				alert("필수 입력 필드를 모두 작성해주세요.");
				return false;
			}
			document.frm.submit();
		}
	</script>
</head>
<body>
	<form name="frm" method="post" action="boardWriteSave.do">
		<table>
			<caption>게시판 등록</caption>
			<tr>
				<th width="20%"><label for="title"></label>제목 </th>
				<td width="80%"><input type="text" name="title" required="required"></td>
			</tr>
			<tr>
				<th><label for="pass"></label>암호 </th>
				<td><input type="password" name="pass" required="required"></td>
			</tr>
			<tr>
				<th>글쓴이 </th>
				<td><input type="text" name="name" required="required"></td>
			</tr>
			<tr>
				<th>내용</th>
				<td><textarea name="content" class="textarea" required="required"></td>
			</tr>
			<tr align="center">
				<td colspan="2">
					<button type="submit" onclick="fn_submit(); return false;">저장</button>
					<button type="reset">취소</button>
			</tr>
		</table>
	</form>
</body>
</html>

 

=> 우리가만든 패키지를 프레임워크에서 인식 못함

dispathcer-servlet에서 수정해줘야함

그리고 context-common

 

 

 insert끝!


 

이제

제이쿼리 추가해주고

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
		table {
			width: 400px;
			border-collapse: collapse;
		}
		th, td {
			border: 1px solid #cccccc;
			padding: 3px
		}
		.textarea{
			width: 98%;
			height: 70px;
		}
	</style>
	<script src="/egov_board/script/jquery.js"></script>
	<script type="text/javascript">
// 		function fn_submit() {
// 			if(document.frm.title.value==""){
// 				alert("제목을 입력해주세요.");
// 				document.frm.title.focus();
// 				return false;
// 			}
// 			document.frm.submit();
// 		}
		function fn_submit() {
		
			if ($.trim($("#title").val()) == "") {
				alert("제목을 입력해 주세요!");
				$("#title").focus();
				return false;
			}
			
// 			if ($("#pass").val() == "") {
			if ($.trim($("#pass").val()) == "") {
				alert("암호를 입력해 주세요!");
				$("#pass").focus();
				return false;
			}
// 			document.frm.submit();
// 			serialize : 폼 자체를 변수로 사용하기 위해서
			var formData = $("#frm").serialize();
			$.ajax({
				 type:"post"
				,data:formData
				,url:"boardWriteSave.do"
				,dataType:"text"
				,success: function(data) {
					if (data == "ok") {
						alert("저장완료");
					}else {
						alert("저장실패");
					}
				}
				,error: function() {
					alert("오류발생");
				}
			});
		}
	</script>
</head>
<body>
<!-- 	<form name="frm" method="post" action="boardWriteSave.do"> -->
	<form id="frm">
		<table>
			<caption>게시판 등록</caption>
			<tr>
				<th width="20%"><label for="title"></label>제목</th>
				<td width="80%"><input type="text" id="title" name="title"></td>
			</tr>
			<tr>
				<th><label for="pass"></label>암호</th>
				<td><input type="password" id="pass" name="pass"></td>
			</tr>
			<tr>
				<th>글쓴이</th>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<th>내용</th>
				<td><textarea name="content" class="textarea"></textarea> </td>
			</tr>
			<tr align="center">
				<td colspan="2">
					<button type="submit" onclick="fn_submit(); return false;">저장</button>
					<button type="reset">취소</button>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

js추가하고 id추가후 함수 바꿔줌,


 

selectList 목록보기

dao

public List<?> selectBoardList(BoardVO vo) throws Exception {
	      return list("boardDAO.selectBoard", vo);
	}

service

List<?> selectBoardList(BoardVO vo) throws Exception;

impl

@Override
	public List<?> selectBoardList(BoardVO vo) throws Exception {
		return boardDAO.selectBoardList(vo);
	}

 

sql

 <select id="boardDAO.selectBoard" resultClass="BoardVO">
        <![CDATA[
            SELECT 
             unq
			,title
			,pass
			,name
			,content
			,hits
			,to_char(rdate,'YYYY-MM-DD') as rdate
             from 
             nboard
        ]]>
    </select>

 

controller

	@RequestMapping(value = "/boardList.do")
	public String selectBoardList(BoardVO vo, ModelMap model) throws Exception {
		
		List<?> list = boardService.selectBoardList(vo);
		log.info("@# list=>"+list);
		model.addAttribute("resultList", list);
		
		return "board/boardList";
	}

boardList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
	<style type="text/css">
		table{
			width: 700px;
			border-collapse: collapse;
		}
		th, td{
			border: 1px solid #cccccc;
			padding: 5px;
		}
	</style>
</head>
<body>
	<table>
		<caption>게시판</caption>
		<tr>
			<th>번호</th>
			<th>제목</th>
			<th>글쓴이</th>
			<th>등록일</th>
			<th>조회수</th>
		</tr>
		<c:set var="count" value="1"/>
		<c:forEach var="result" items="${resultList}" varStatus="status">
            <tr align="center">
				<td><c:out value="${count}"/></td>
				<td align="left">${result.title}</td>
				<td>${result.name}</td>
				<td>${result.rdate}</td>
				<td>${result.hits}</td>
            </tr>
            <c:set var="count" value="${count+1}"/>
        </c:forEach>
	</table>
	<div style="width: 700px; margin-top: 5px; text-align: right;">
		<button type="button" onclick="location='boardWrite.do'">글쓰기</button>
	</div>
</body>
</html>

 

디버그로그찍기

info=>debug
새로고침하면 이제 쿼리가 보인다