|
1. Vispirms uzrakstiet darbību paketes darbība; publiskā klase InterceptorAction { public String fun1() { return "ok"; }}
2. Darbības konfigurēšana <package name="base" extends="struts-default"> <global-results> <result name="message">/index.jsp</result> <result name="message">/WEB-INF/page/message.jsp </result> </global-results>< </package> pakotnes nosaukums="delversi" namespace="/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" method="{1}"> <interceptor-ref name="permission"></interceptor-ref> <result name="ok">/ddd.jsp</result> </action>
Pārtvērēju kaudze ir definēta tā, lai varētu izmantot sistēmas iebūvētos pārtvērējus
3. Uzrakstiet atļauju bloķētāju paku pārtvērējs; importēt com.opensymphony.xwork2.ActionContext; importēt com.opensymphony.xwork2.ActionInvocation; importēt com.opensymphony.xwork2.interceptor.Interceptor; public class InterceptorDemo implements Interceptor { public void destroy() { // TODO Automātiski ģenerēta metode stub } public void init() { // TODO Automātiski ģenerēta metode stub } public String intercept(ActionInvocation invocation) met Exception { System.out.println("Ievadiet pārtvērēju"); } if (ActionContext.getContext().getSession().get("user") !=null ) { return invocation.invoke(); }else { ActionContext.getContext().put("ziņojums", "bez atļaujas"); atgriezties "ziņojums"; } }}Pieteicies, atgriezās labi un atgrieza ziņojumu, ja nē
4. Uzrakstiet jsp un ievadiet lietotāju sesijā <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% request.getSession().setAttribute("lietotājs","111");%>
Lūdzu, norādiet atkārtotas drukāšanas avotuhttp://blog.csdn.net/z1104222568/article/details/42218125
|