|
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" namespace="/test" extends="base"> <interceptors> <име на прехващач="permission" class="interceptor.InterceptorDemo"></interceptor> <interceptor-stack name="permissionStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="разрешение"></interceptor-ref> </interceptor-stack> </interceptors> <име на действие="interceptor_*" class="action.InterceptorAction" метод="{1}"> <interceptor-ref name="permission"></interceptor-ref> <result name="ok">/ddd.jsp</result> </action>
Стекът на прехващачи е дефиниран така, че да може да използва вградените в системата интерцептори
3. Напишете блокер на разрешения прехващач на пакети; импорт com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class InterceptorDemo имплементира Interceptor { public void destroy() { // TODO Auto-generated method stub } public void init() { // TODO Auto-generated method stub } public String intercept(ActionInvocation invocation) throws Exception { System.out.println("Enter interceptor"); } if (ActionContext.getContext().getSession().get("user") !=null ) { return invocation.invoke(); }else { ActionContext.getContext().put("message", "no permission"); обратно "съобщение"; } }}Влязох в системата, върнах ОК и върнах съобщение, ако не
4. Напишете jsp и инжектирайте user в сесията <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% request.getSession().setAttribute("user","111");%>
Моля, посочете източника за преиздаванеhttp://blog.csdn.net/z1104222568/article/details/42218125
|