단순 코드 기록/Spring

Spring_Intercept

일일일코_장민기 2024. 2. 14. 11:13
728x90
Controller

 

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public String main() {
	    System.out.println("main");
	    return "main";
	}
	
	//member/**로 요청할 경우, interceptor가 동작
	//세션 검사를 통해 회원|비회원 여부 판단
	//회원일 경우 --> 제대로 동작
	//비회원일 경우 --> 로그인 화면으로 이동
	
	@RequestMapping(value = "/aaa", method = RequestMethod.GET)
	public String aaa() {
	    System.out.println("aaa");
	    return "main";
	}
	
	
}

 

 

MyHandlerInterceptor

 

package com.intercept;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class MyHandlerInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
		System.out.println("preHandle");
		return true;	//true면 계속 진행, false면 진행 금지
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception{
		System.out.println("postHandle");		
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception{
		System.out.println("afterCompletion");		
	}
	
}

 

Servlet-context.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<resources mapping="/resources/**" location="/resources/" />

	<beans:bean id="xxx" class="com.controller.HomeController"></beans:bean>
	<beans:bean id="myHandlerInterceptor" class="com.intercept.MyHandlerInterceptor"></beans:bean>

	<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
		<beans:property name="interceptors">
			<beans:list>
				<beans:ref bean="myHandlerInterceptor"/>
			</beans:list>
		</beans:property>	
	</beans:bean>

	<annotation-driven />
	
	<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>
	
	
	
</beans:beans>

 

 

 

'단순 코드 기록 > Spring' 카테고리의 다른 글

Spring_보안폴더에서 외부파일 사용+xml주소처리  (0) 2024.02.14
Spring_Intercept_for_login  (0) 2024.02.14
Spring_Redirect_Forwarding  (0) 2024.02.14
Spring_URLMAPPING  (0) 2024.02.14
Spring_Exception  (0) 2024.02.13