ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring] 설정 및 구현 - 생명주기, MVC
    Server/Spring 2020. 4. 12. 23:47

    목차

    🍓  1. 빈(Bean) 객체의 생명주기(Life Cycle)

    🍓  2. 어노테이션을 이용한 스프링 설정

    🍓  3. 웹 프로그래밍 설계 모델

    🍓  4. 스프링 MVC 웹서비스 

    🍓  5. STS를 이용하지 않은 웹 프로젝트

    🍓  6. Service & Dao 객체 구현 

    🍓  7. Controller 객체 구현 

     

     

     

     

    🍓  1. 빈(Bean) 객체의 생명주기(Life Cycle)

    스프링 컨테이너 생성시점과 빈객체의 생성시점은 동일하다.

     

     

     

     

     

    🍓  2. 어노테이션을 이용한 스프링 설정

     

     

     

     

    🍓 3. 웹 프로그래밍 설계 모델

    13-1 웹프로그래밍을 구축하기 위한설계모델 

    13-2 스프링MVC플레임워크 설계 구조 

    13-3 DispatcherServlet 설정 

    13-4 Controller 객체 -@Controller 

    13-5 Controller 객체 -@RequestMapping 

    13-6 Controller 객체 –Model 타입의 파라미터 

    13-7 View 객체 

    13-8 전체적인 웹프로그래밍 구조 

     

    동작

    1. Client -> DispatcherServlet 으로 요청 (Request )

    2. DispatcherServlet -> HandlerMapping 가장 적합한 Controller를 찾음

    3. HandlerAdapter -> Controller 요청처리 -> DispatcherServlet 찾아준 Controller 안에 가장 적합한 메소드 찾음

    4. DispatcherServlet -> ViewResolver 처리결과에 해당하는 View(JSP)가 어떤 것인지 찾아줌

    5. DispatcherServlet -> View 응답 생성

    6. View -> Client 화면에 응답

     

     

     

     

     

     

    🍓 4. 스프링 MVC 웹서비스 

     STS를이용한 웹프로젝트 생성 

    15-1 프로젝트 전체구조 

    15-2 web.xml 

    15-3 DispatcherServlet 

    15-4 servlet-context.xml 

    15-5 Controller(컨트롤러) 

    15-6 View(뷰) 

     

     

    web.xml

    • DispatcherServlet 설정
    <!-- Processes application requests -->
    	<servlet>
    		<servlet-name>appServlet</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    		
    	<servlet-mapping>
    		<servlet-name>appServlet</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    • 서버에 한글깨지지 않도록 설정
    <filter>
    		<filter-name>encodingFilter</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>
    		<init-param>
    			<param-name>forceEncoding</param-name>  
    			<param-value>true</param-value>
    		</init-param>
    	</filter>    
    
    	<filter-mapping>
    		<filter-name>encodingFilter</filter-name>
    		<url-pattern>/*</url-pattern>                 
    	</filter-mapping>

     

    web.xml

    서블릿으로 등록 될 때contextConfigLocation이름으로초기화 파라미 터를 servlet-context.xml로 지정하고 있는데 이때 지정된 servlet-context.xml파일이 스프링 설정의 역할을 하는 파일이다. 

    <init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    		</init-param>

     

     

    servlet-context.xml

    프로젝트에 필요한 객체(빈:bean) 를생성하고 조립한다.

    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<beans:property name="prefix" value="/WEB-INF/views/" />
    		<beans:property name="suffix" value=".jsp" />
    	</beans:bean>

     

     

     

    🍓 7. Controller 객체 구현 

    18-1 웹어플리케이션 준비 

    18-2 @RequestMapping을이용한 URL맵핑 

    18-3 요청파라미터

    19-1 @ModelAttribute 

    19-2 커맨드객체 프로퍼티 데이터타입 

    19-3 Model & ModelAndViewspring 

     

     

    Controller -> Service -> Dao

     

     

    MemberController.java

    @Controller
    @RequestMapping("/member")
    public class MemberController {
    
    	@Autowired
    	MemberService service;
        
        @RequestMapping(value = "/memJoin", method = RequestMethod.POST)
    	public String memJoin(Member member) {
    		
    		service.memberRegister(member);
    		
    		return "memJoinOk";
    	}
    }

     

     

    MemberService.java

    public interface MemberService{
    	void memberRegister( Member member );
    }

     

    MemberServiceImpl.java

    @Service
    public class MemberServiceImpl implements MemberService{
    	@AutoWired
        MemberDao dao;
        
        @Override
        public void memberRegister (Member member)
        {
        	printMembers(dao.memberInsert(member));
        }
    }

     

     

    MemberDao.java

    public interface MemberDao{
    	Map<String, Member> memberInsert (Member member);
    }

     

    MemberDaoImpl.java

    @Repository
    public class MemberDaoImpl implements MemberDao{
    	private HashMap<String, Member> dbMap;
        
        public MemberDaoImpl(){
        	dbMap = new HashMap<String, Member>
        }
        
        @Override
        public Map<String, Member> memberInsert (Member member){
        	dbMap.put(member.getMemId(), member);
            return dbMap;
        }
    }

     

     

     

     

     

     

     

    반응형
Designed by Tistory.