- 로그인 예제
- 파일 업로드, 다운로드 예제
파일 업로드 관련 라이브러리
\spring-framework-3.0.2.RELEASE-dependencies\org.apache.commons\com.springsource.org.apache.commons.io\1.4.0폴더에서
com.springsource.org.apache.commons.fileupload-1.2.0.jar 복사 후 lib에 추가.
[ 소스경로 ]
(1) web.xml
<?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>spring06_members</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 인코딩필터 설정하기 -->
<filter>
<filter-name>characterEncoding</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>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- /// applicationContext.xml을 읽어와 빈으로 등록하는 리스너 설정 /// -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- //////////////////// DispatcherServlet설정 //////////////////// -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- /////////////////////////////////////////////////////////////// -->
</web-app>
(2) 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">
<!-- //// 전역적인 자원들이 등록 (Dao,Service,...) ////// -->
<!-- 1. DataSource 설정(url,username,password,..) -->
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!-- 2. SqlSessionFactory설정(dataSource,Mapper파일설정) -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:test/mybatis/mybatis-config.xml"/>
</bean>
<!-- 3. SqlSessionTemplate설정(SqlSessionFactory주입) -->
<bean id="sqlSessionTemplate"
class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" index="0"/>
</bean>
<!-- 파일 업로드 dao, service설정 -->
<bean id="fileinfoDao" class="test.dao.FileinfoDao">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
</bean>
<!-- service설정 -->
<bean id="fileinfoService" class="test.service.FileinfoService">
<property name="dao" ref="fileinfoDao"/>
</bean>
<!-- 로그인 dao, service 설정 -->
<bean id="loginDao" class="test.dao.LoginDao">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
</bean>
<!-- service설정 -->
<bean id="loginService" class="test.service.LoginService">
<property name="dao" ref="loginDao"/>
</bean>
</beans>
(3) mvc-servlet.xml
- multipartResolver : 파일 업로드 관련
- 다운로드 창 설정
- Resolver 에 우선순위를 부여. 우선 순위가 높은 순서로 찾아간다.
<?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"
xsi:schemaLocation="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-3.1.xsd">
<!-- @autowired 만 적용할려면 -->
<context:annotation-config/>
<bean class="test.controller.FileuploadController"/>
<bean class="test.controller.FileListController"/>
<bean class="test.controller.FileDownloadController"/>
<bean class="test.controller.LoginController"/>
<bean class="test.controller.FileDeleteController"/>
<!--///////// 파일업로드 관련 시작 //////////////-->
<!-- id속성이 반드시 multipartResolver 이어야 함. !! -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 최대업로드 크기 설정(바이트단위) -->
<property name="maxUploadSize" value="5242880"/>
</bean>
<!--///////// 파일업로드 관련 끝 //////////////-->
<!-- 다운로드 뷰 생성(커스텀뷰) -->
<bean id="filedownloadView" class="test.view.FiledownloadView"/>
<!--///////// 뷰리절버등록 ///////////-->
<!-- 뷰이름으로 뷰를 찾아가는 뷰리절버 등록 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="0"/><!-- 리절버중에서 실행순서. 0순위 -->
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"></property>
<property name="order" value="1"/><!-- 리절버중에서 실행순서. 1순위 -->
</bean>
</beans>
(4) login.jsp
- id, password 체크
<%@ 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>login.jsp</title>
</head>
<body>
<form action="login.do" method="post">
아이디<input type="text" name="id"/><form:errors path="command.id"/><br/>
비밀번호<input type="password" name="pwd"/><form:errors path="command.pwd"/><br/>
<input type="submit" value="로그인"/>
</form>
</body>
</html>
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)
(16)
(17)
(18)
(19)