JAVA/Spring Framework
알바 관리 메이븐 프로젝트에 Spring 적용하기
Tech Signal
2020. 10. 12. 19:22
- pom.xml 수정
- web.xml
- contextParam
- filter, Listener 추가
- frontController -> appServlet으로 대체
- WEB-INF 내에 spring폴더 만들기 (spring의 웹 리소스들 만들기)
- spring-appServlet 폴더에 Spring configuration xml 파일 만들기( 파일명: servlet-context.xml)
- namespaces 추가 (c, p, context, beans, mvc 등)
- namespace를 mvc를 기본설정으로 하고 beans 에서 beans:beans로 바꾼다 (optional)
- context:component scan 추가 후 controller, controllerAdvice 만 include
- <annotation-driven /> (mvc:anno하거나)
- <InternalResourceViewResolver> 추가하고 prefix, suffix 지정 ( for .jsp files)
- <default-servlet-handler/>, localeResolver를 위한 인터셉터, messageSource 등 추가
- localeResolver 를 추가함으로써 알바 관리에서 한글인 컬럼이름들을 영어로 변경 가능
- root-context.xml에서는 controller, controllerAdvice만 빼고 모든 @component 추가
- VO에서 PartWrapper를 포함하는 요소들 다 MultipartFile 로 수정, 파일명도 기존의 메서드 지우고 UUID에서 randomUUID 생성해 넣기
- Dao 인터페이스에서 더이상 SqlSession 사용하지 않으니 시그니처 수정
- (대신 xml에서 sqlSessionTemplate 사용해 트랜잭션 관리함)
- daoImpl 다 삭제
- Service 구현체
- @service 붙이고 인터페이스엔 안붙임
- 생성자 삭제
- dao에서 더이상 sqlSession 안쓰니 try with resource 문 안에서 아예 세션 만들고 session.commit()할 필요도 없음. 지우기
- Controller 단 수정
- @Controller 붙이기
- RequestParameter -> RequestParam으로 고치기
- HTTPServletRequest-> Model 로 바꾸기
- Model 객체로 바꾸는 이유:
- ProdRetrieveController: 알바생 목록 보여주는 메서드에서 json 마샬링하는 부분 따로 메서드 분리
- @RequestMapping에서 produces 속성으로 mediaType이 jSON일 경우에 매핑 설정
- ProdInsertController
- validation 필요하니까 @Validated(InsertGroup.Class) 적용
- BindingResults 타입의 errors 받아오기
- 요청이 Multipart 타입일때 사진 저장하던 부분은
- CodeRetrieveController
- 의 경우 갖고 있는 메서드 2개 다 마샬링을 해야함
- resp.sendError
ServletContext 가 필요한 부분은 private WebApplicationContext context.getServletContext()로 하기