[struts/servletConfig] 인터셉터(initerceptor) – servletConfig

2022. 2. 23. 23:58비즈니스 탐색/개발

728x90
반응형

 

# 모든 액션을 만들 때 기본적으로 들어가는 것.

package test.action;
import com.opensymphony.xwork2.Action;
public class LoginProAction implements Action{
public String execute() throws Exception {
return SUCCESS;
}
}

 

(중요) servletConfig

 

기존에 mvc패턴에서 쓰던 서블릿(realPath, request set Attribute, session 등) 을 넣어주는 작업을 하는 것으로

 

struts2 Servlet API 사용하지 않으므로 특별히 ServletContext, HttpServletRequest, HttpServletResponse등의 서블릿 객체에 접근을 하기 위해서 또는 Parameter Map(요청파라미터의 이름값을 저장한 ), Request Map(HttpServletRequest attribute 이름값을 저장한 ), Session Map(HttpSession Attribute 이름값을 저장한 ), Application Map(ServletContext Attribute 이름 저장등이 필요한 경우 servletConfig Interceptor 이용하면 된다.

 

이러한 Map에 접근을 하기 위해서는 적절한 Map을 가져다 쓰면 되는데…

예를들어 세션에 있는 attribute에 접근을 원한다고 HttpSession 객체를 만들어 attribute에 접근할 필요 없이 직접 Session Map에 접근해서 원하는 속성을 가져다 쓰면 된다는 뜻이다.

Action이 servletConfig 인터셉터로부터 서블릿 객체나 Map을 주입받기 위해서는 각각의 Aware인터페이스를 구현해야 한다.

 

 

servletConfig 예시

 

# form.jsp 생성

# struct.xml 액션 추가

<action name="loginPro" class="test.action.LoginProAction">

<interceptor-ref name="servletConfig" />

<result>/0907/loginPro.jsp</result>

</action>

 

<interceptor-ref name="servletConfig" />

sevletConfig가 하는 작업은 7개의 데이터를 넣어주는 것!!

(1) ServletContextAware : ServletContext 객체를 받을 수 있다.
(2) ★(중요) ServletRequestAware : HttpServeltRequest 객체를 받을 수 있다. : jstl에서 넣어주던 request.이것을 넣으면 mvc패턴에서 쓰던 것처럼 사용할 수 있다. 

 

(3) ServletResponseAware : HttpServeltResponse 객체를 받을 수 있다.
(4) ParameterAware : Parameter Map을 받을 수 있다. : 파라미터 맵. 전달받은 파라미터를 맵으로 바로 넣어주는 것으로 잘 안쓰인다. 전달받은 것은 DTO로 넣기 때문.
(5) RequestAware : Request Map을 받을 수 있다.
(6) SessionAware : Session Map을 받을 수 있다. 
(7) ApplicationAware : Application Map을 받을 수 있다. 

 

# LoginProAction 생성

# loginPro.jsp 생성

# form.jsp 실행

728x90
반응형