BoardVO.java
package vo;
public class BoardVO {
private int boardIdx;
private int boardSeq;
private String boardType;
private int step;
private String title;
private String content;
private String regiDt;
private String regiUser;
private String modiDt;
private String modiUser;
public int getBoardIdx() {
return boardIdx;
}
public void setBoardIdx(int boardIdx) {
this.boardIdx = boardIdx;
}
public int getBoardSeq() {
return boardSeq;
}
public void setBoardSeq(int boardSeq) {
this.boardSeq = boardSeq;
}
public String getBoardType() {
return boardType;
}
public void setBoardType(String boardType) {
this.boardType = boardType;
}
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
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 getRegiDt() {
return regiDt;
}
public void setRegiDt(String regiDt) {
this.regiDt = regiDt;
}
public String getRegiUser() {
return regiUser;
}
public void setRegiUser(String regiUser) {
this.regiUser = regiUser;
}
public String getModiDt() {
return modiDt;
}
public void setModiDt(String modiDt) {
this.modiDt = modiDt;
}
public String getModiUser() {
return modiUser;
}
public void setModiUser(String modiUser) {
this.modiUser = modiUser;
}
}
|
DetailController.java
package controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import logic.Board;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import vo.BoardVO;
public class DetailController extends AbstractController {
private Board board = null;
public void setBoard(Board board) {
this.board = board;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest req,
HttpServletResponse res) throws Exception {
// TODO Auto-generated method stub
int boardIdx = RequestUtils.getIntParameter(req, "boardIdx");
BoardVO boardDetail = board.getBoardDetail(boardIdx);
Map model = new HashMap();
model.put("boardDetail", boardDetail);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("detail");
modelAndView.addAllObjects(model);
return modelAndView;
}
}
|
.AbstractController 클래스
-Controller 인터페이스를 구현항 추상 클래스
-다음과 같은 프로퍼티 설정이 가능
프로퍼티 |
디폴트 |
설명 |
supportedMethods |
GET, POST |
.허가하는 리퀘스트 메소드를 정의
.쉼표 구분으로 복수 지정 가능
.위반 시 Request MethodNotSupported Exception throw |
requireSession |
False |
.Session의 존재가 필수인지 아닌지를 지정
.Session이 확인되지 않을 경우에는 SessionRequired Exception throw |
cacheSession |
-1 |
.HTTP header에 기재하는 cache의 유효 기간을 초단위로 지정
.'0' : No-cache
.'-1' : cache header를 작성하지 않음 |
synchronizeOnSession |
False |
.HttpSession 오브젝트 상에서 handleRequestInternal()메소드의 호출을 동기화할지 안 할지는 지정 |
*** 위 프로퍼티에 관해서는 나중에 따로 정리를 하겠음.
IndexController.java
package controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import logic.Board;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.sun.org.apache.xpath.internal.axes.HasPositionalPredChecker;
public class IndexController implements Controller {
private Board board = null;
public void setBoard(Board board) {
this.board = board;
}
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
List boardList = board.getBoardList();
Map model = new HashMap();
model.put("boardList", boardList);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addAllObjects(model);
modelAndView.setViewName("board");
return modelAndView;
}
}
|
Board.java
package logic;
import java.util.List;
import vo.BoardVO;
public interface Board {
List getBoardList();
BoardVO getBoardDetail(int boardIdx);
}
|
BoardImpl.java
package logic;
import java.util.List;
import vo.BoardVO;
import dao.BoardDao;
public class BoardImpl implements Board {
private BoardDao boardDao = null;
public void setBoardDao(BoardDao boardDao) {
this.boardDao = boardDao;
}
@Override
public List getBoardList() {
// TODO Auto-generated method stub
return boardDao.getBoardList();
}
@Override
public BoardVO getBoardDetail(int boardIdx) {
// TODO Auto-generated method stub
return boardDao.getBoardDetail(boardIdx);
}
}
|
BoardDao.java
package dao;
import java.util.List;
import vo.BoardVO;
public interface BoardDao {
List getBoardList();
BoardVO getBoardDetail(int boardIdx);
}
|
BoardDaoImpl.java
package dao;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import vo.BoardVO;
public class BoardDaoImpl extends SqlMapClientDaoSupport implements BoardDao {
@Override
public List getBoardList() {
// TODO Auto-generated method stub
return getSqlMapClientTemplate().queryForList("board.getBoardList");
}
@Override
public BoardVO getBoardDetail(int board_idx) {
// TODO Auto-generated method stub
return (BoardVO)getSqlMapClientTemplate().queryForObject("board.getBoardDetail", board_idx);
}
}
|
web.xml
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 1. dataSource 생성 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:orcl"></property>
<property name="username" value="xxxxxxx"></property>
<property name="password" value="yyyyyyy"></property>
</bean>
<!-- 2. ibatis 코딩을 지원해주는 SqlMapClient 빈 생성 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="/WEB-INF/SqlMapConfig.xml"></property>
</bean>
<!-- 각종 빈 생성 : myHome01-servlet.xml에서 설정 -->
</beans>
|
/* 보통 applicationContext.xml에는 해당 프로젝트에서 공통으로 사용하는 bean을 생성 */
myHome01-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- HandlerMapping 설정 -->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/board.html">indexController</prop>
<prop key="/detail.html">detailController</prop>
</props>
</property>
</bean>
<!-- ViewResolver 설정 -->
<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- 각종 빈 생성 -->
<bean id="indexController" class="controller.IndexController">
<property name="board" ref="board"></property>
</bean>
<bean id="detailController" class="controller.DetailController">
<property name="board" ref="board"></property>
</bean>
<bean id="board" class="logic.BoardImpl">
<property name="boardDao" ref="boardDao"></property>
</bean>
<bean id="boardDao" class="dao.BoardDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
</beans>
|
.SimpleUrlHandlerMapping 클래스
-Properties형의 mappings 프로퍼티를 갖고 있음
-리퀘스트 url과 리퀘스트 controller를 관련지은 Properties 오브젝트를 설정
.InternalResourceViewResolver 클래스
-viewClass, prefix, suffix 프로퍼티 설정이 가능
-viewClass : View 인터페이스를 구현한 클래스를 정의
--JstlView 클래스 : JSTL을 사용하여 JSP 페이지 작성을 지원하는 View 인터페이스의 구현 클래스
-prefix : 전문자
-suffix : 후문자
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<sqlMap resource="../classes/sql/oracle/board.xml"/>
</sqlMapConfig>
|
/* ibatis 사용을 위한 설정파일 */
board.xml
<?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">
<!-- Mapper 파일 -->
<!--
이름 충돌을 방지하기 위해 namespace를 사용
SqlMapConfig.xml에 <settings useStatementNamespaces="true"/> 설정이 지정되어 있는 경우
sql구문을 찾을 때 반드시 namespace값을 포함해야 한다.
-->
<sqlMap namespace="board">
<select id="getBoardList" resultClass="vo.BoardVO">
SELECT BOARD_IDX AS BOARDIDX,
BOARD_SEQ AS BOARDSEQ,
BOARD_TYPE AS BOARDTYPE,
STEP AS STEP,
TITLE AS TITLE,
CONTENT AS CONTENT,
TO_CHAR(REGI_DT, 'YYYY-MM-DD') AS REGIDT,
REGI_USER AS REGIUSER,
TO_CHAR(MODI_DT, 'YYYY-MM-DD') AS MODIDT,
MODI_USER AS MODIUSER
FROM BOARD
ORDER BY BOARD_IDX DESC, BOARD_SEQ DESC
</select>
<select id="getBoardDetail" parameterClass="int" resultClass="vo.BoardVO">
SELECT BOARD_IDX AS BOARDIDX,
BOARD_SEQ AS BOARDSEQ,
BOARD_TYPE AS BOARDTYPE,
STEP AS STEP,
TITLE AS TITLE,
CONTENT AS CONTENT,
TO_CHAR(REGI_DT, 'YYYY-MM-DD') AS REGIDT,
REGI_USER AS REGIUSER,
TO_CHAR(MODI_DT, 'YYYY-MM-DD') AS MODIDT,
MODI_USER AS MODIUSER
FROM BOARD
WHERE BOARD_IDX = #value#
</select>
</sqlMap>
|
index.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>BOARD LIST</title>
<script type="text/javascript">
function goDetail(board_idx){
var doc = document.boardForm;
doc.boardIdx.value = board_idx;
//alert(board_idx);
doc.action = "./detail.html";
doc.submit();
}
</script>
</head>
<body>
<form name="boardForm">
<input type="hidden" name="boardIdx" value=""/>
<table border="1">
<tr>
<th width="50">No.</th>
<th width="400">제목</th>
<th width="100">등록일</th>
<th width="100">등록자</th>
</tr>
<c:forEach items="${boardList}" var="board">
<tr>
<td align="center"> <c:out value="${board.boardIdx}"></c:out> </td>
<td align="left" onclick="goDetail('<c:out value="${board.boardIdx}"></c:out>')"> <c:out value="${board.title}"></c:out> </td>
<td align="center"> <c:out value="${board.regiDt}"></c:out> </td>
<td align="center"> <c:out value="${board.regiUser}"></c:out> </td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
|
detail.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>BOARD LIST</title>
</head>
<body>
<table border="1">
<tr>
<th width="50">No.</th>
<th width="400">제목</th>
<th width="100">등록일</th>
<th width="100">등록자</th>
</tr>
<tr>
<td align="center"> <c:out value="${boardDetail.boardIdx}"></c:out> </td>
<td align="left"> <c:out value="${boardDetail.title}"></c:out> </td>
<td align="center"> <c:out value="${boardDetail.regiDt}"></c:out> </td>
<td align="center"> <c:out value="${boardDetail.regiUser}"></c:out> </td>
</tr>
</table>
<input type="button" value=" list " onclick="window.location='board.html'" />
</body>
</html>
|
[참고자료] Spring 2.5 실무 프로그래밍(성윤정)