/WEB-INF/pages/emp/form.jsp
<%@ page language="java" isELIgnored="false" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ko">
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<style>
.error {
color: #ff0000;
}
.errorblock{
color: #000;
background-color: #ffEEEE;
border: 3px solid #ff0000;
padding:8px;
margin:16px;
}
</style>
<script type="text/javascript" language="JavaScript">
function showMessage(msg){
if(msg != ''){
alert(msg);
}
}
function list(){
location.href="/emp/list";
}
</script>
</head>
<body >
<form:form commandName="emp" name="form" method="post" enctype="multipart/form-data" action="/emp/register">
<h2> 사원정보 등록 화면 </h2>
<table width="280" border="0">
<tr>
<td align="left"> empno : </td>
<td>
<input type="text" id="empno" name="empno" value="${emp.empno}" maxlength="20" size="20"/>
<br><form:errors path="empno" cssClass="error" />
</td>
</tr>
<tr>
<td align="left"> ename : </td>
<td>
<input type="text" id="ename" name="ename" value="${emp.ename}" maxlength="20" size="20"/>
<br><form:errors path="ename" cssClass="error" />
</td>
</tr>
<tr>
<td align="left"> job : </td>
<td><input type="text" id="job" name="job" value="${emp.job}" maxlength="20" size="20"/></td>
</tr>
<tr>
<td align="left"> sal : </td>
<td>
<input type="text" id="sal" name="sal" value="${emp.sal}" maxlength="20" size="20"/>
<br><form:errors path="sal" cssClass="error" />
</td>
</tr>
<tr>
<td align="left"> deptno : </td>
<td>
<select name="deptno">
<option value="10" <c:if test="${emp.deptno == 10}">selected</c:if>>10</option>
<option value="20" <c:if test="${emp.deptno == 20}">selected</c:if>>20</option>
<option value="30" <c:if test="${emp.deptno == 30}">selected</c:if>>30</option>
</select>
</td>
</tr>
<tr>
<td align="left"> 첨부파일 </td>
<td>
<p><input type="file" name="filename" id="filename1" size="35" /></p>
<p><input type="file" name="filename" id="filename2" size="35" /></p>
<p><input type="file" name="filename" id="filename3" size="35" /></p>
</td>
</tr>
<tr height="10"><td></td></tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="저장" />
<input type="button" value="취소" onclick="list()"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
com.oracleclub.study.mvc.model.Emp.java
package com.oracleclub.study.mvc.model;
import java.util.Date;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
/**
*
* emp 테이블 모델객체
*
* @author : oramaster
*
*/
public class Emp {
private int empno;
private int mgr;
private int sal;
private int deptno;
private String ename;
private String job;
private Date hiredate;
private CommonsMultipartFile[] filename;
public CommonsMultipartFile[] getFilename() {
return filename;
}
public void setFilename(CommonsMultipartFile[] filename) {
this.filename = filename;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
/WEB-INF/mvc-dispatcher-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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<context:component-scan base-package="com.oracleclub.study" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order" value="0" />
</bean>
<!-- Spring FileUpload 처리 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="1000000"/>
</bean>
<!-- config.properties 파일 로딩 (파일업로드 경로등의 환경설정) -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config.properties" />
</bean>
</beans>
/src/resources/config.properties
uploadPath = ${uploadPath}
/filter/build-local.filter
uploadPath = C:/stsworkspace/spring-project/upload
com.oracleclub.study.mvc.controller.EmpRegisterController.java
package com.oracleclub.study.mvc.controller;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.oracleclub.study.mvc.model.Emp;
/**
* 사원정보 저장을 처리
*
* @author : gurubee
*
*/
@Controller
@RequestMapping(value = "/emp")
public class EmpRegisterController implements InitializingBean {
private static final Log LOG = LogFactory.getLog(EmpRegisterController.class);
/**
* 파일을 저장 할 경로
*/
@Value("file:${uploadPath}")
private Resource uploadPath;
@RequestMapping(value = "/form", method = RequestMethod.GET)
public String form() {
return "emp/form";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView register(@ModelAttribute Emp emp) {
LOG.debug(" #### 사원정보 : " + emp);
// 파일업로드
if (emp.getFilename() != null) {
writeFile(emp.getFilename());
}
// DB저장하고
// View 설정
ModelAndView mv = new ModelAndView();
mv.setViewName("emp/view");
return mv;
}
/**
* 파일 업로드 처리
*
* @param multipartFiles
*/
private void writeFile(CommonsMultipartFile[] multipartFiles) {
OutputStream out = null;
try {
for (CommonsMultipartFile multipartFile : multipartFiles) {
out = new FileOutputStream(uploadPath.getFile().getAbsolutePath() + "/"
+ multipartFile.getOriginalFilename());
BufferedInputStream bis = new BufferedInputStream(multipartFile.getInputStream());
byte[] buffer = new byte[8106];
int read;
while ((read = bis.read(buffer)) > 0) {
out.write(buffer, 0, read);
}
IOUtils.closeQuietly(out);
}
} catch (IOException ioe) {
LOG.error(ioe);
} finally {
IOUtils.closeQuietly(out);
}
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(uploadPath, "FileUpload Path must be defined!");
LOG.debug(" ######### uploadPath : " + uploadPath.getFile().getAbsolutePath());
// 디렉토리가 존재하지 않는다면, 디렉토리를 만든다.
if (!uploadPath.getFile().exists()) {
uploadPath.getFile().mkdirs();
}
}
}