셋팅하기 이전의 전제조건
- 오라클이 깔려 있을것
- 이클립스 버전은 최신이여도 된다. 단 아래는 2.5를 기준으로 설정해봤다.
1. 이클립스에서 아래와 같이 Dynamic Web Project를 생성
2. Tomcat 라이브러리 폴더에다 오라클 jdbc 파일을 넣는다.
3. JNDI를 이용한 jdbc 설정.
- Servers 의 serve.xml파일에다가 아래와 같이 추가.
-->
<GlobalNamingResources>
...................
<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver"
maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/SpringDB" password="tiger" type="javax.sql.DataSource" url="jdbc:oracle:thin:@localhost:1521:ora" username="scott"/>
</GlobalNamingResources>
- 그리고 같은 파일 맨 마지막에 위의 Resource를 link할수 있게 context아래에 아래와 같이 설정한다.
<Context docBase="SpringBoard" path="/SpringBoard" reloadable="true" source="org.eclipse.jst.jee.server:SpringBoard">
<ResourceLink name="jdbc/SpringDB" type="javax.sql.DataSource" global="jdbc/SpringDB" />
</Context>
4. 파일 구조.
5. 소스
- BoardController.java
package ck.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import ck.dao.BoardDao;
public class BoardController extends AbstractController {
private BoardDao boardDao;
public void setBoardDao(BoardDao boardDao){
this.boardDao = boardDao;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest req,
HttpServletResponse res) throws Exception {
// TODO Auto-generated method stub
ModelAndView mav = new ModelAndView();
mav.setViewName("list");
mav.addObject("board", "This is the Test");
mav.addObject("boardCount", new Integer(boardDao.count()));
return mav;
}
}
- BoardDao.java
package ck.dao;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.sql.DataSource;
public class BoardDao {
private DataSource ds;
public void setDataSource(DataSource ds){
this.ds = ds;
}
public int count() throws Exception{
int result = -1;
Connection con = null;
Statement stmt = null;
try{
con = ds.getConnection();
stmt = con.createStatement();
String sql = "select count(*) as CNT from BOARD";
ResultSet rs = stmt.executeQuery(sql);
rs.next();
result = rs.getInt("CNT");
}
finally{
close(con, stmt);
}
return result;
}
private void close(Connection con, Statement stmt){
try{
if(con != null) con.close();
}catch(Exception err){
}
try{
if(stmt != null) stmt.close();
}catch(Exception err){
}
}
}
- web.xml (/WEB-INF)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HelloApp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springapp-servlet.xml</param-value>
</context-param>
</web-app>
- springapp-servle.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- the application context definition for the springapp DispatcherServlet -->
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix">
<beans:value>/board/</beans:value>
</beans:property>
<beans:property name="suffix">
<beans:value>.jsp</beans:value>
</beans:property>
</beans:bean>
<beans:bean name="/board_list.do" class="ck.web.BoardController">
<beans:property name="boardDao" ref="boardDao"></beans:property>
</beans:bean>
<beans:bean id = "boardDao" class="ck.dao.BoardDao">
<beans:property name="dataSource" ref="dataSource"></beans:property>
</beans:bean>
<beans:bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<beans:property name="jndiName" value="jdbc/SpringDB"></beans:property>
<beans:property name="resourceRef" value="true"></beans:property>
</beans:bean>
</beans:beans>
- index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Board Test</h2>
<hr></hr>
<a href="board_list.do">Go to the list</a>
</body>
</html>
- list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${requestScope.board}
<p></p>
Total cnt : ${boardCount}
</body>
</html>
'프로그래밍 > SPRING 2.5' 카테고리의 다른 글
스프링의 구조 (0) | 2013.09.05 |
---|---|
스프링 컨트롤러 계층 구조 (0) | 2013.09.05 |
DispatcherServlet (0) | 2013.09.04 |
스프링 MVC 라이프 사이클 (0) | 2013.09.04 |
스프링 MVC개요 (0) | 2013.09.04 |