Struts2에서 인터셉터의 개념을보고 있습니다. Struts2에 대한 튜토리얼에서 아래 예제를 보았습니다.Struts 2 인터셉터 내부의 HttpSession에 어떻게 액세스합니까? 해야합니까?
이 예제에서 작성자는 인터셉터를 사용하여 HttpSession
에 액세스하여 자격 증명을 확인하고 USER_KEY
을 확인합니다.
요격기 자체에서 HttpSession
에 액세스하는 것은 좋지 않은가요? 인터셉터는 Java EE 프로그래밍에서 간단한 서블릿 필터라고 생각합니다.
HttpSession
에 액세스하는 가장 좋은 장소는 Struts2의 액션 클래스 안에 있다고 느낍니다.
내가 잘못했을 경우를 대비하여 저를 시정하십시오.
public class AuthorizationInterceptor extends AbstractInterceptor {
private static final String USER_KEY = "user";
public String intercept(ActionInvocation invocation) throws Exception {
Map session = invocation.getInvocationContext().getSession();
if (session.get(USER_KEY) == null) {
addActionError(invocation, "You must be authenticated to access this page");
return Action.ERROR;
}
return invocation.invoke();
}
private void addActionError(ActionInvocation invocation, String message) {
Object action = invocation.getAction();
if (action instanceof ValidationAware) {
((ValidationAware) action).addActionError(message);
}
}
}
이것은 당신도 도움이 될 수 있습니다 : http://stackoverflow.com/questions/5509606/session-problem-in- struts2-application/5517526 # 5517526 –