SingleUpDTO sto = new SingleUpDTO(); sto.setComment(request.getParameter("comment")); sto.setUpfile(upfile); HashMap map = new HashMap(); map.put("sto", sto); map.put("list", list); return new ModelAndView("single_res",map); } @RequestMapping("/multiUp.do") public ModelAndView multiUp(MultiupDTO mto) throws Exception{ String comment = mto.getComment(); List<MultipartFile> list = mto.getUpfile(); for(MultipartFile file : list){ if(!file.isEmpty()){ String fileName = file.getOriginalFilename(); long size = file.getSize(); System.out.println(fileName+" - "+size); //파일 이동(파일 카피가 아님) file.transferTo(new File(uploadDirectory,fileName)); } } System.out.println("comment : "+comment); return new ModelAndView("multi_res","mto",mto);
} //다운 로드 처리 @RequestMapping("/download.do") public ModelAndView download(String fileName) throws Exception{ return new ModelAndView("downloadView", "fileName", fileName); }
public class SingleUpDTO { private String comment; private MultipartFile upfile;//업로드된 파일 정보를 저장할 변수 public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public MultipartFile getUpfile() { return upfile; } public void setUpfile(MultipartFile upfile) { this.upfile = upfile; }
/* * View 클래스 작성 * 1. AbstractView를 extends * 2. getContentType() : 응답 content-type을 알려주는 메소드 * renderMergedOutputModel - 응답처리 * 오버라이딩 * */ public class DownloadView extends AbstractView{
@Override public String getContentType() { return "application/octet-stream";//application인데 stream을 통해서 보내긴 하겠지만 뭔지 모르겟다.따라서 이걸 부르면 다운로드 받게 해준다. } //1. 인수 - Map : ModelAndView의 Model을 Map으로 넘겨준다. @Override protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws Exception {//map은 우리가 modelAndView로 넘기는 값이 들어온다. //map : fileName 키값으로 다운로드 시킬 파일 명을 받는다. ServletContext ctx = getServletContext(); //webapplication 경로-> filesystem결로로 바꿔준다. String realPath = ctx.getRealPath("/upload");///upload : web application Root경로 //파일경오만 바뀌지 다른 것은 그대로 가져다가 재사용가능하다.
String fileName = (String) map.get("fileName"); File downFile = new File(realPath, fileName); //응답처리 //1. contentType처리 response.setContentType(getContentType()); //2. 다운 로드할 파일 명을 header정보로 설정 response.setHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("UTF-8"),"8859_1"));//8859-1방식은 라틴방식이다. (한글처리) //위에서 filename으로 파일이 넘어간다. OutputStream os = response.getOutputStream(); FileInputStream fi = new FileInputStream(downFile); //fi.read()한것을 os.write한 효과가 난다. 클라이언트에게 파일이 날아간다. FileCopyUtils.copy(fi, os); }
public class SingleUpDTO { private String comment; private MultipartFile upfile;//업로드된 파일 정보를 저장할 변수 public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public MultipartFile getUpfile() { return upfile; } public void setUpfile(MultipartFile upfile) { this.upfile = upfile; }
}
<%@ 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> 업로드된 파일<br> Comment : ${requestScope.comment }<br> 파일 : ${requestScope.fileName } <hr> singleUpDTO.do 응답처리<br> Comment : ${requestScope.sto.comment }<br> 파일 : ${requestScope.sto.upfile.originalFilename } <hr> singleUpReq.do 응답처리<br> Comment : ${requestScope.sto2.comment }<br> 파일 : ${requestScope.sto2.upfile.originalFilename } </body> </html>
SingleUpDTO sto = new SingleUpDTO(); sto.setComment(request.getParameter("comment")); sto.setUpfile(upfile); HashMap map = new HashMap(); map.put("sto", sto); map.put("list", list); return new ModelAndView("single_res",map); }
1. 호출 JSP(또는 HTML) - 요청 방식 : post - <form enctype="multipart/form-data"> - input tag : <input type="file" name="upfile"/> - name 송석의 값은 upload정보를 저장할 DTO(VO)의 Attribute와 매칭 된다. - 여러 개의 파일을 업로드 할 때 name속성의 값은 이름[0], 이름[1] 형식으로 작성
1. multipartResolver 빈으로 등록 - upload를 처리해 주는 bean - id/name은 반드시 multipartResolver로 등록 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> - property - defaultEncoding - 기본 인코딩 설정 - maxUploadSize - 업로드 허용 최대 size를 byte단위로 지정. -1은 무제한 - uploadTempDir - 업로드 파일이 저장될 임시 경로 지정 - maxInMemorySize - 업로드 파일을 저장할 최대 메모리 크기
* FileUpload - Controller에서 처리
1. DTO를 통해 받기 - 파일 요청 파라미터의 이름과 매칭되는 property작성 - 파일의 정보를 저장할 property는 MultipartFile 타입으로 작성
2. @RequestParam 을 통해 받기 - Controller 메소드의 MultipartFile 타입의 매개변수 사용
1. org.springframework.web.multipart.MultipartFile - 업로드된 파일정보를 저장하는 객체 - getName() : String -요청파라미터의 name - getOriginalFilename() :String - upload된 파일명 - getSize() : long - 파일의 크기 - transferTo(File dest) - upload된 파일을 특정 경로로 이동 - isEmpty() : boolean - upload된 파일이 없으면 true - getInputStream() : InputStream - 업로드 된 파일과 연결된 InputStream 리턴
* 원래 파일 업로드를 실행하려면 복잡한대 이걸 처리해주는 api가 있고 이걸 등록해서 사용하면 된다.
시작 : login_success.jsp, menu.jsp(getLoginMemberInfo.do) ctr : getLoginMemberInfo() view : 성공 : member_detail.jsp 로그인 안한 경우 : login_form.jsp
회원정보 수정 폼
시작 : member_details.jsp, menu.jsp(modifyForm.do) ctr : modifyForm() view : 성공 : /res/modify_form.jsp 로그인 안한경우 : login_form.jsp
회원정보 수정 처리 시작 : modifyForm.jsp( modifyMember.do ) Ctr : modifyMember() Service : modifyMember() ->dao view : 성공 : /res/member_detail.jsp 실패 : exception - error.jsp 로그인 안한 경우 - login_form.jsp
회원 탈퇴 처리
시작 : member_detail.jsp, menu.jsp( removeMember.do ) Ctr : removeMember(HttpSession) Service : removeMemberById() View : 성공 - login_form.jsp 실패 - Exception - res/error.jsp 로그인 안한 상태 - login_form.jsp
검색처리
시작 : search_form.jsp ( /searchMemberByMileageRange.do ) Ctr : searchMemberByMileageRange(int startMileage. int endMileage) Service : searchMemberByMileageRange() ->dao view : 성공 : /res/search_result.jsp 실패 : exception - res/error.jsp
이름으로 검색처리
시작 : search_form.jsp ( /searchMemberByName.do ) Ctr : searchMemberByName(String name) service : searchMemberByName() =>dao view : 성공 : /res/search_result.jsp 실패 : exception : res/error.jsp
import memver.model.service.MemberManageService; @Controller public class MemberController { private MemberManageService service; public MemberController(MemberManageService service){ this.service = service; } @RequestMapping(value="/registerMember.do") public ModelAndView registerMember(MemberDTO mdto){ ModelAndView mv = null; mdto.setMileage(1000); try { System.out.println(mdto); service.registerMember(mdto); System.out.println("****************"); mv = new ModelAndView("/res/register_success","mdto",mdto); } catch (Exception e) { e.printStackTrace(); mv = new ModelAndView("/res/error","error_message",e.getMessage()); } return mv; } @RequestMapping(value="/getAllMemberInfo.do") public ModelAndView getAllMemberInfo(){ ModelAndView mv = null; try{ System.out.println("getMemberList()실행"); ArrayList<MemberDTO> list = service.getMemberList(); mv = new ModelAndView("/member_list","member_list",list); }catch (Exception e) { mv = new ModelAndView("/res/error", "error_message",e.getMessage()); } return mv; } @RequestMapping(value="/login.do") public ModelAndView login(String id, String password, HttpSession session){
ModelAndView mv = null; try { MemberDTO mdto = service.getMemberById(id); if(mdto!=null){//id가 있는 경우 if(mdto.getPassword().equals(password)){//id와 password가 맞는 경우 System.out.println("login_success.jsp실행"); session.setAttribute("memberDTO", mdto); mv = new ModelAndView("/res/login_success"); }else{//id가 맞고 비밀번호가 틀린경우 mv = new ModelAndView("/login_form","error_message",password+"는 없는 비밀번호입니다."); } }else{//id와 비밀번호가 틀린경우 mv = new ModelAndView("/login_form","error_message",id+"는 없는 아이디입니다."); } } catch (SQLException e) { e.printStackTrace(); mv = new ModelAndView("/res/error","error_message",e.getMessage()); } return mv; } @RequestMapping(value="/logout.do") public ModelAndView logout(HttpSession session){ ModelAndView mv = null; MemberDTO mdto = (MemberDTO) session.getAttribute("memberDTO"); if(mdto!=null){ mv = new ModelAndView("/login_form"); session.invalidate(); }else{ mv = new ModelAndView("/login_form","error_message","로그인 후 이용바랍니다."); } return mv; } @RequestMapping(value="getLoginMemberInfo.do") public ModelAndView getLoginMemberInfo(HttpSession session){ ModelAndView mv = null; MemberDTO mdto = (MemberDTO) session.getAttribute("memberDTO"); if(mdto!=null){ mv = new ModelAndView("/res/member_details"); }else{ mv = new ModelAndView("/login_form","error_message","로그인 후 이용바랍니다."); } return mv; } @RequestMapping("modifyForm.do") public ModelAndView modifyForm(HttpSession session){ ModelAndView mv = null; MemberDTO mdto = (MemberDTO) session.getAttribute("memberDTO"); if(mdto!=null){ mv = new ModelAndView("/res/modify_form"); }else{ mv = new ModelAndView("/login_form","error_message","로그인 후 이용바랍니다."); } return mv; } @RequestMapping("/modifyMember.do") public ModelAndView modifyMember(MemberDTO mdto, HttpSession session){ ModelAndView mv = null; System.out.println(mdto); MemberDTO login_info = (MemberDTO) session.getAttribute("memberDTO"); if(login_info==null){//로그인 안된 경우 mv = new ModelAndView("/login_form","error_message","로그인 후 회원정보 수정바랍니다."); }else{//로그인 된 경우 try { System.out.println("update처리"); service.modifyMember(mdto); // login_info.setPassword(mdto.getPassword()); session.setAttribute("memberDTO", mdto); mv = new ModelAndView("/res/member_details"); } catch (SQLException e) { e.printStackTrace(); mv = new ModelAndView("/res/error"); } } return mv; } @RequestMapping("removeMember.do") public ModelAndView removeMember(HttpSession session){ MemberDTO mdto = (MemberDTO) session.getAttribute("memberDTO"); ModelAndView mv = null; if(mdto==null){//로그인 안된 경우 mv = new ModelAndView("/login_form","error_message","로그인 후 회원 탈퇴 이용바랍니다."); }else{ try { service.removeMemberById(mdto.getId()); mv = new ModelAndView("/login_form"); //세션 소멸시키기 session.invalidate(); } catch (SQLException e) { e.printStackTrace(); mv = new ModelAndView("/res/error"); } } return mv; } @RequestMapping("searchMemberByMileageRange.do") public ModelAndView searchMemberByMileageRange(int startMileage, int endMileage,HttpServletRequest request){ ModelAndView mv = null; ArrayList<MemberDTO> list = null;
try { list = service.selectMemberByMileageRange(startMileage, endMileage); request.setAttribute("title","마일리지검색"); request.setAttribute("search_value", startMileage+"~"+endMileage);
System.out.println(list); mv = new ModelAndView("/res/search_result","searchList",list); } catch (SQLException e) { e.printStackTrace(); mv = new ModelAndView("/res/error","error_messge",e.getMessage()); } return mv; } @RequestMapping("searchMemberByName.do") public ModelAndView searchMemberByName(String name, HttpServletRequest request){ ModelAndView mv = null; ArrayList<MemberDTO> list = null;
try { list = service.getSearchByMemberName(name); request.setAttribute("title","이름으로 검색"); request.setAttribute("search_value", name); System.out.println(list); mv = new ModelAndView("/res/search_result","searchList",list); } catch (SQLException e) { e.printStackTrace(); mv = new ModelAndView("/res/error","error_messge",e.getMessage()); } return mv; } }
package member.dto;
public class MemberDTO { private String id; private String password; private String name; private String registerNumber1; private String registerNumber2; private int mileage; public MemberDTO() { super(); } public MemberDTO(String id, String password, String name, String registerNumber1, String registerNumber2, int mileage) { super(); this.id = id; this.password = password; this.name = name; this.registerNumber1 = registerNumber1; this.registerNumber2 = registerNumber2; this.mileage = mileage; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRegisterNumber1() { return registerNumber1; } public void setRegisterNumber1(String registerNumber1) { this.registerNumber1 = registerNumber1; } public String getRegisterNumber2() { return registerNumber2; } public void setRegisterNumber2(String registerNumber2) { this.registerNumber2 = registerNumber2; } public int getMileage() { return mileage; } public void setMileage(int mileage) { this.mileage = mileage; } @Override public String toString() { return "MemberDTO [id=" + id + ", password=" + password + ", name=" + name + ", registerNumber1=" + registerNumber1 + ", registerNumber2=" + registerNumber2 + ", mileage=" + mileage + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + mileage; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((registerNumber1 == null) ? 0 : registerNumber1.hashCode()); result = prime * result + ((registerNumber2 == null) ? 0 : registerNumber2.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; MemberDTO other = (MemberDTO) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; if (mileage != other.mileage) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (registerNumber1 == null) { if (other.registerNumber1 != null) return false; } else if (!registerNumber1.equals(other.registerNumber1)) return false; if (registerNumber2 == null) { if (other.registerNumber2 != null) return false; } else if (!registerNumber2.equals(other.registerNumber2)) return false; return true; }
}
package member.exception;
public class DuplicatedIdException extends Exception{ public DuplicatedIdException(){} public DuplicatedIdException(String message){ super(message); } }
package member.exception;
public class DuplicatedRegisterNumberException extends Exception{ public DuplicatedRegisterNumberException(){} public DuplicatedRegisterNumberException(String message){ super(message); } }
import member.dto.MemberDTO; import member.exception.DuplicatedIdException; import member.exception.DuplicatedRegisterNumberException; import member.model.dao.MemberDAO; //회원 관리 비지니스 로직을 처리하는 Business Service 클래스 public class MemberManageService { private MemberDAO dao; public MemberManageService(MemberDAO dao){ this.dao = dao; }
//가입 처리 메소드 public void registerMember(MemberDTO mto)throws SQLException, DuplicatedIdException, DuplicatedRegisterNumberException{
//1. 등록된 ID가 있는 지 체크 // - 이미 등록된 ID라면 DuplicatedIdException를 발생시킨다. if(dao.selectMemberById(mto.getId())!=null){ throw new DuplicatedIdException(mto.getId()+"는 이미등록된 아이디 입니다."); }
//2. 등록된 주민번호가 있는 지 체크 // - 이미 등록된 주민번호라면 DuplicatedRegisterNumberException 발생시킨다. if(dao.selectMemberByRegisterNumber(mto.getRegisterNumber1(), mto.getRegisterNumber2())!=null){ throw new DuplicatedRegisterNumberException("는 이미 가입된 주민번호 입니다."); }
//3. 디비에 회원정보 등록 처리
dao.insertMember(mto);
} //전체 회원목록 조회 public ArrayList<MemberDTO> getMemberList() throws SQLException{ return dao.selectAllMember(); } //마일리지 범위로 조회 public ArrayList<MemberDTO> selectMemberByMileageRange(int startMileage, int endMileage)throws SQLException { return dao.selectMemberByMileageRange(startMileage, endMileage); } //ID로 회원 목록조회 public MemberDTO getMemberById(String id) throws SQLException{ return dao.selectMemberById(id); } //회원정보 수정 //update메소드 public void modifyMember(MemberDTO mto) throws SQLException{ int cnt = dao.updateMember(mto); System.out.println(cnt+"개의 행이 업데이트 되었습니다.");
} public void removeMemberById(String id)throws SQLException { int cnt = dao.deleteMemberById(id); System.out.println(cnt+"개의 행이 삭제되었습니다."); } public ArrayList<MemberDTO> getSearchByMemberName(String name) throws SQLException{ return dao.searchByMemberName(name);
} }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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> <jsp:include page="/menu.jsp"/> <c:if test="${requestScope.error_message!=null }"> 실행 도중 오류가 발생했습니다.<br> 오류 내용 ${error_message } </c:if> <a href='/${initParam.context_root }/index.jsp'>메인페이지로 이동</a> </body> </html>
<%@page import="member.dto.MemberDTO"%> <%@ 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> <script type="text/javascript"> function modifyCheck(){
var pwd = document.modify_form.password; var name = document.modify_form.name; var registerNumber1 = document.modify_form.registerNumber1; var registerNumber2 = document.modify_form.registerNumber2;
<%@ 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> <script type="text/javascript"> function mileageSearchFormCheck(){ var startMileage = document.mileage_search_form.startMileage; var endMileage = document.mileage_search_form.endMileage;
import member.dto.MemberDTO; import member.exception.DuplicatedIdException; import member.exception.DuplicatedRegisterNumberException; import memver.model.service.MemberManageService; @Controller public class MemberController { private MemberManageService service; public MemberController(MemberManageService service){ this.service = service; } @RequestMapping(value="/registerMember.do") public ModelAndView registerMember(MemberDTO mdto){ ModelAndView mv = null; mdto.setMileage(1000); try { System.out.println(mdto); service.registerMember(mdto); System.out.println("****************"); mv = new ModelAndView("/res/register_success","mdto",mdto); } catch (Exception e) { e.printStackTrace(); mv = new ModelAndView("/res/error","error_message",e.getMessage()); } return mv; } @RequestMapping(value="/getAllMemberInfo.do") public ModelAndView getAllMemberInfo(){ ModelAndView mv = null; try{ System.out.println("getMemberList()실행"); ArrayList<MemberDTO> list = service.getMemberList(); mv = new ModelAndView("/member_list","member_list",list); }catch (Exception e) { mv = new ModelAndView("/res/error", "error_message",e.getMessage()); } return mv; } @RequestMapping(value="/login.do") public ModelAndView login(String id, String password, HttpSession session){ ModelAndView mv = null; try { MemberDTO mdto = service.getMemberById(id); if(mdto!=null){ if(mdto.getPassword().equals(password)){ System.out.println("login_success.jsp실행"); session.setAttribute("memberDTO", mdto); mv = new ModelAndView("/res/login_success"); }else{ mv = new ModelAndView("/login_form","error_message",password+"는 없는 비밀번호입니다."); } }else{ mv = new ModelAndView("/login_form","error_message",id+"는 없는 아이디입니다."); } } catch (SQLException e) { e.printStackTrace(); mv = new ModelAndView("/res/error","error_message",e.getMessage()); } return mv; } @RequestMapping(value="/logout.do") public ModelAndView logout(HttpSession session){ ModelAndView mv = null; MemberDTO mdto = (MemberDTO) session.getAttribute("memberDTO"); if(mdto!=null){ mv = new ModelAndView("/login_form"); session.invalidate(); }else{ mv = new ModelAndView("/login_form","error_message","로그인 후 이용바랍니다."); } return mv; }
}
package member.dto;
public class MemberDTO { private String id; private String password; private String name; private String registerNumber1; private String registerNumber2; private int mileage; public MemberDTO() { super(); } public MemberDTO(String id, String password, String name, String registerNumber1, String registerNumber2, int mileage) { super(); this.id = id; this.password = password; this.name = name; this.registerNumber1 = registerNumber1; this.registerNumber2 = registerNumber2; this.mileage = mileage; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRegisterNumber1() { return registerNumber1; } public void setRegisterNumber1(String registerNumber1) { this.registerNumber1 = registerNumber1; } public String getRegisterNumber2() { return registerNumber2; } public void setRegisterNumber2(String registerNumber2) { this.registerNumber2 = registerNumber2; } public int getMileage() { return mileage; } public void setMileage(int mileage) { this.mileage = mileage; } @Override public String toString() { return "MemberDTO [id=" + id + ", password=" + password + ", name=" + name + ", registerNumber1=" + registerNumber1 + ", registerNumber2=" + registerNumber2 + ", mileage=" + mileage + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + mileage; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((registerNumber1 == null) ? 0 : registerNumber1.hashCode()); result = prime * result + ((registerNumber2 == null) ? 0 : registerNumber2.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; MemberDTO other = (MemberDTO) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; if (mileage != other.mileage) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (registerNumber1 == null) { if (other.registerNumber1 != null) return false; } else if (!registerNumber1.equals(other.registerNumber1)) return false; if (registerNumber2 == null) { if (other.registerNumber2 != null) return false; } else if (!registerNumber2.equals(other.registerNumber2)) return false; return true; }
}
package member.exception;
public class DuplicatedIdException extends Exception{ public DuplicatedIdException(){} public DuplicatedIdException(String message){ super(message); } }
package member.exception;
public class DuplicatedRegisterNumberException extends Exception{ public DuplicatedRegisterNumberException(){} public DuplicatedRegisterNumberException(String message){ super(message); } }
import member.dto.MemberDTO; import member.exception.DuplicatedIdException; import member.exception.DuplicatedRegisterNumberException; import member.model.dao.MemberDAO; //회원 관리 비지니스 로직을 처리하는 Business Service 클래스 public class MemberManageService { private MemberDAO dao; public MemberManageService(MemberDAO dao){ this.dao = dao; }
//가입 처리 메소드 public void registerMember(MemberDTO mto)throws SQLException, DuplicatedIdException, DuplicatedRegisterNumberException{
//1. 등록된 ID가 있는 지 체크 // - 이미 등록된 ID라면 DuplicatedIdException를 발생시킨다. if(dao.selectMemberById(mto.getId())!=null){ throw new DuplicatedIdException(mto.getId()+"는 이미등록된 아이디 입니다."); }
//2. 등록된 주민번호가 있는 지 체크 // - 이미 등록된 주민번호라면 DuplicatedRegisterNumberException 발생시킨다. if(dao.selectMemberByRegisterNumber(mto.getRegisterNumber1(), mto.getRegisterNumber2())!=null){ throw new DuplicatedRegisterNumberException("는 이미 가입된 주민번호 입니다."); }
import member.dto.MemberDTO; import member.exception.DuplicatedIdException; import member.exception.DuplicatedRegisterNumberException; import memver.model.service.MemberManageService; @Controller public class MemberController { private MemberManageService service; public MemberController(MemberManageService service){ this.service = service; } @RequestMapping(value="/registerMember.do") public ModelAndView registerMember(MemberDTO mdto){ ModelAndView mv = null; mdto.setMileage(1000); try { System.out.println(mdto); service.registerMember(mdto); System.out.println("****************"); mv = new ModelAndView("/res/register_success","mdto",mdto); } catch (Exception e) { e.printStackTrace(); mv = new ModelAndView("/res/error","error_message",e.getMessage()); } return mv; } @RequestMapping(value="/getAllMemberInfo.do") public ModelAndView getAllMemberInfo(){ ModelAndView mv = null; try{ System.out.println("getMemberList()실행"); ArrayList<MemberDTO> list = service.getMemberList(); mv = new ModelAndView("/member_list","member_list",list); }catch (Exception e) { mv = new ModelAndView("/res/error", "error_message",e.getMessage()); } return mv; } }
package member.dto;
public class MemberDTO { private String id; private String password; private String name; private String registerNumber1; private String registerNumber2; private int mileage; public MemberDTO() { super(); } public MemberDTO(String id, String password, String name, String registerNumber1, String registerNumber2, int mileage) { super(); this.id = id; this.password = password; this.name = name; this.registerNumber1 = registerNumber1; this.registerNumber2 = registerNumber2; this.mileage = mileage; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRegisterNumber1() { return registerNumber1; } public void setRegisterNumber1(String registerNumber1) { this.registerNumber1 = registerNumber1; } public String getRegisterNumber2() { return registerNumber2; } public void setRegisterNumber2(String registerNumber2) { this.registerNumber2 = registerNumber2; } public int getMileage() { return mileage; } public void setMileage(int mileage) { this.mileage = mileage; } @Override public String toString() { return "MemberDTO [id=" + id + ", password=" + password + ", name=" + name + ", registerNumber1=" + registerNumber1 + ", registerNumber2=" + registerNumber2 + ", mileage=" + mileage + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + mileage; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((registerNumber1 == null) ? 0 : registerNumber1.hashCode()); result = prime * result + ((registerNumber2 == null) ? 0 : registerNumber2.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; MemberDTO other = (MemberDTO) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; if (mileage != other.mileage) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (registerNumber1 == null) { if (other.registerNumber1 != null) return false; } else if (!registerNumber1.equals(other.registerNumber1)) return false; if (registerNumber2 == null) { if (other.registerNumber2 != null) return false; } else if (!registerNumber2.equals(other.registerNumber2)) return false; return true; }
}
package member.exception;
public class DuplicatedIdException extends Exception{ public DuplicatedIdException(){} public DuplicatedIdException(String message){ super(message); } }
package member.exception;
public class DuplicatedRegisterNumberException extends Exception{ public DuplicatedRegisterNumberException(){} public DuplicatedRegisterNumberException(String message){ super(message); } }
import member.dto.MemberDTO; import member.exception.DuplicatedIdException; import member.exception.DuplicatedRegisterNumberException; import member.model.dao.MemberDAO; //회원 관리 비지니스 로직을 처리하는 Business Service 클래스 public class MemberManageService { private MemberDAO dao; public MemberManageService(MemberDAO dao){ this.dao = dao; }
//가입 처리 메소드 public void registerMember(MemberDTO mto)throws SQLException, DuplicatedIdException, DuplicatedRegisterNumberException{
//1. 등록된 ID가 있는 지 체크 // - 이미 등록된 ID라면 DuplicatedIdException를 발생시킨다. if(dao.selectMemberById(mto.getId())!=null){ throw new DuplicatedIdException(mto.getId()+"는 이미등록된 아이디 입니다."); }
//2. 등록된 주민번호가 있는 지 체크 // - 이미 등록된 주민번호라면 DuplicatedRegisterNumberException 발생시킨다. if(dao.selectMemberByRegisterNumber(mto.getRegisterNumber1(), mto.getRegisterNumber2())!=null){ throw new DuplicatedRegisterNumberException("는 이미 가입된 주민번호 입니다."); }