티스토리 뷰

728x90
반응형

사전 정보

java 21.0.4 2024-07-16 LTSJava(TM) SE Runtime Environment (build 21.0.4+8-LTS-274)

Java HotSpot(TM) 64-Bit Server VM (build 21.0.4+8-LTS-274, mixed mode, sharing)

 

Tomcatv 10.1

EclipseVersion: 2024-06 (4.32.0)Build id: 20240606-1231


pom.xml 설정

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>6.1.11</version>
</dependency>

ojdbc.jar 설정

WEB-INF -> lib 폴더에 "ojdbc.jar" 파일을 추가합니다.

ojdbc11.jar
6.87MB


web.xml 설정

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="https://jakarta.ee/xml/ns/jakartaee"
	xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
	id="WebApp_ID" version="5.0">
	<display-name>com.mvc</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.jsp</welcome-file>
		<welcome-file>default.htm</welcome-file>
	</welcome-file-list>
	
	
	<!-- DispatcherServlet 설정 시작. -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/applicationContext.xml</param-value>
		</init-param>
		<!-- 값이 1 이상이면 톰켓(Tomcat) 실행시 DispatcherServlet 을 미리 메모리에 로드합니다. -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- url-pattern 설정 -->
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<!-- DispatcherServlet 설정 끝. -->


	<!-- 한글 깨짐 설정(텍스트 인코딩) 시작. -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 한글 깨짐 설정(텍스트 인코딩) 끝. -->

</web-app>

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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans 
           https://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context 
           https://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc 
           https://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- spring MVC annotation(@)을 사용하기 위한 설정 -->
	<mvc:annotation-driven />

	<!-- base-package 속성에 지정된 패키지와 그 하위 패키지에 있는 모든 클래스를 스캔합니다. -->
	<context:component-scan base-package="com.mvc.controller" />

	<!-- controller 를 처리할 view 위치 및 확장자명 설정 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- 데이터 소스 설정 시작. -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@localhost:1521/xe" />
		<property name="username" value="system" />
		<property name="password" value="1234" />
	</bean>
	<!-- 데이터 소스 설정 끝. -->
	
	<!-- 빈 정의 시작. -->
	<bean id="memberDAOImp" class="com.mvc.member.MemberDAOImp">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<bean id="memberController" class="com.mvc.controller.MemberController">
        <constructor-arg name="memberDAO" ref="memberDAOImp" />
    </bean>
    <!-- 빈 정의 끝. -->

</beans>

MemberDAO.java

package com.mvc.member;

public interface MemberDAO {
	public void memberList();
}

MemberDAOImp.java

package com.mvc.member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.sql.DataSource;

public class MemberDAOImp implements MemberDAO {
	private Connection connection;
	private PreparedStatement preparedStatement;
	private DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public void memberList() {
		try {
			this.connection = this.dataSource.getConnection();

			String query = "select * from t_member";
			this.preparedStatement = this.connection.prepareStatement(query);

			ResultSet resultSet = this.preparedStatement.executeQuery();
			while (resultSet.next()) {
				System.out.println(resultSet.getString("name"));
			}

			resultSet.close();
			this.preparedStatement.close();
			this.connection.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}

MemberController.java

package com.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.mvc.member.MemberDAO;

@Controller
public class MemberController {
	private final MemberDAO memberDAO;

	public MemberController(MemberDAO memberDAO) {
		this.memberDAO = memberDAO;
	}

	@RequestMapping(value = "/member/list", method = RequestMethod.GET)
	public ModelAndView memberList() {

		this.memberDAO.memberList();
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("user_id", "jh.jang");
		modelAndView.setViewName("/member/memberList");

		return modelAndView; // WEB-INF/views/member/memberList.jsp로 이동
	}

	@RequestMapping(value = "/member/add", method = RequestMethod.GET)
	public String memberAdd() {
		return "/member/memberAdd"; // // WEB-INF/views/member/memberAdd.jsp로 이동
	}

	@RequestMapping(value = "/member/edit", method = RequestMethod.GET)
	public String memberEdit() {
		return "/member/memberEdit"; // // WEB-INF/views/member/memberEdit.jsp로 이동
	}

	@RequestMapping(value = "/member/delete", method = RequestMethod.GET)
	public String memberDelete() {
		return "/member/memberDelete"; // // WEB-INF/views/member/memberDelete.jsp로 이동
	}

}

호출 및 결과

http://localhost:8080/member/list


오류 메세지

"Cannot find class [org.springframework.jdbc.datasource.DriverManagerDataSource]" 오류 메세지가 발생한다면

"pom.xml 설정" 작업을 진행하세요.

 

"Failed properties: Property 'driverClassName' threw exception" 오류 메세지가 발생한다면

"ojdbc.jar 설정" 작업을 진행하세요.


추가 정보

메이븐(Maven)

 

메이븐(Maven)

메인븐(Maven)이란? Maven은 프로젝트를 표준화된 방식으로 관리하고, 의존성을 자동으로 처리하며, 빌드 과정을 자동화하는 데 도움을 줍니다.pom.xml 파일을 통해 프로젝트의 의존성,

jangjeonghun.tistory.com

스프링 프레임워크(Spring FrameWordk)-Spring Web MVC

 

스프링 프레임워크(Spring FrameWordk)-Spring Web MVC

사전 정보java 21.0.4 2024-07-16 LTSJava(TM) SE Runtime Environment (build 21.0.4+8-LTS-274)Java HotSpot(TM) 64-Bit Server VM (build 21.0.4+8-LTS-274, mixed mode, sharing) Tomcat v10.1Eclipse Version: 2024-06 (4.32.0) Dynamic Web Pr

jangjeonghun.tistory.com

 

728x90
반응형