|
1. 먼저 행동을 작성한다 패키지 액션; public class InterceptorAction { public String fun1() { return "ok"; }}
2. 액션 구성 <패키지 이름="base" extends="struts-default"> <global-results> <결과 이름="message">/index.jsp</result> <결과 이름="message">/WEB-INF/page/message.jsp </result> </global-results><패키지 </package> 이름="delversi" 네임스페이스="/test" extends="base"> <interceptors> <interceptor name="permission" class="interceptor.InterceptorDemo"></interceptor> <interceptor-stack name="permissionStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="permission"></interceptor-ref> </interceptor-stack> </interceptors> <action name="interceptor_*" class="action.InterceptorAction" 메서드="{1}"> <interceptor-ref name="permission"></interceptor-ref> <결과 이름="ok">/ddd.jsp</result> </action>
인터셉터 스택은 시스템 내장 인터셉터를 사용할 수 있도록 정의되어 있습니다
3. 허가 차단기 작성 패키지 인터셉터; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class InterceptorDemo는 Interceptor를 구현합니다 { public void destroy() { // TODO 자동 생성 메서드 stub } public void init() { // TODO 자동 생성 메서드 stub } public 문자열 intercept(ActionInvocation invocation) Exception throws { System.out.println("Enter interceptor"); } if (ActionContext.getContext().getSession().get("user") !=null ) { return invocation.invoke(); }else { ActionContext.getContext().put("message", "no permission"); "메시지"를 반환; } }}로그인 후 정상으로 돌아왔고, 아니면 메시지를 반환했습니다
4. jsp 작성 후 사용자를 세션에 주입합니다 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% request.getSession().setAttribute("user","111");%>
재인쇄 출처를 명시해 주세요http://blog.csdn.net/z1104222568/article/details/42218125
|