by-nc-sa     개발자, DBA가 함께 만들어가는 구루비 지식창고!

사원 정보등록 기능완성




사원 정보등록 기능완성

1. EmpBO.java 수정

  • EmpBO 클래스에 사원정보를 저장하는 registerEmp 메소드를 추가한다.
com.oracleclub.study.mvc.bo.EmpBO.java
package com.oracleclub.study.mvc.bo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.oracleclub.study.mvc.dao.EmpDAO;
import com.oracleclub.study.mvc.model.Emp;

/**
 * 사원관리 비즈니스로직 처리
 * 
 * @author : oramaster
 * 
 */
@Service
public class EmpBO {

	@Autowired
	private EmpDAO empDAO;

	/**
	 * 사원목록 조회
	 * 
	 * @return
	 */
	public List<Emp> getEmpList() {
		return empDAO.selectEmpList();
	}

	/**
	 * 사원정보 등록
	 * @param emp
	 */
	public void registerEmp(Emp emp) {
		empDAO.insertEmp(emp);
	}
}

2. EmpRegisterController 완성

  • @Autowired로 EmpBO 객체를 생성하고 사원정보를 등록하는 registerEmp 메소드를 호출한다.
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.Autowired;
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.bo.EmpBO;
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;

	@Autowired
	private EmpBO empBO;

	@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저장하고
		empBO.registerEmp(emp);

		// 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();
		}

	}
}

3. 테스트

문서정보

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.