|
1. Najpierw napisz akcję akcja pakietowa; public class InterceptorAction { public String fun1() { return "ok"; }}
2. Konfiguruj akcję <nazwa pakietu="base" extends="struts-default"> <global-results> <nazwa wyniku="message">/index.jsp</result> <nazwa wyniku="message">/WEB-INF/page/message.jsp </result> </global-results><nazwa </package> pakietu="delversi" przestrzeń nazw="/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> <nazwa akcji="interceptor_*" class="action.InterceptorAction" method="{1}"> <interceptor-ref name="permission"></interceptor-ref> <result name="ok">/ddd.jsp</result> </action>
Stos przechwytujący jest definiowany tak, aby mógł korzystać z wbudowanych interceptorów systemu
3. Napisz blokator uprawnień przechwytujący pakiet; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class InterceptorDemo implementuje Interceptor { public void destroy() { // TODO Auto-generated method stub } public void init() { // TODO automatycznie wygenerowana metoda stub } public String intercept(ActionInvocation invocation) rzuca wyjątek { System.out.println("Enter interceptor"); } if (ActionContext.getContext().getSession().get("user") !=null ) { return invocation.invoke(); }else { ActionContext.getContext().put("message", "no permission"); oddając "wiadomość"; } }}Zalogowałem się, zwróciłem poprawnie, a jeśli nie wróciłem, wiadomość
4. Napisz jsp i wstrzykni użytkownika do sesji <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% request.getSession().setAttribute("user","111");%>
Proszę podać źródło przedrukuhttp://blog.csdn.net/z1104222568/article/details/42218125
|