|
1. Najprv napíšte akciu balíková akcia; public class InterceptorAction { public String fun1() { return "ok"; }}
2. Konfigurácia akcie <názov balíka="base" extends="struts-default"> <global-results> <resultname="message">/index.jsp</result> <resultname="message">/WEB-INF/page/message.jsp </result> </global-results><package </package> name="delversi" namespace="/test" extends="base"> <interceptors> <meno interceptora="permission" class="interceptor.InterceptorDemo"></interceptor> <meno interceptor-stack="permissionStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="permission"></interceptor-ref> </interceptor-stack> </interceptors> <názov akcie="interceptor_*" class="action.InterceptorAction" method="{1}"> <interceptor-ref name="permission"></interceptor-ref> <result name="ok">/ddd.jsp</result> </action>
Zásobník interceptorov je definovaný tak, aby mohol využívať zabudované interceptory systému
3. Napíšte blokátor povolení balíkový stíhač; 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 Automaticky generovaná metóda stub } public String intercept(ActionInvocation invocation) hodí výnimku { System.out.println("Enter interceptor"); } if (ActionContext.getContext().getSession().get("user") !=null ) { return invocation.invoke(); }else { ActionContext.getContext().put("message", "no permission"); vrátiť "správu"; } }}Prihlásil som sa, vrátil som v poriadku a ak nie, správu som vrátil
4. Napísať jsp a injektovať používateľa do relácie <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% request.getSession().setAttribute("user","111");%>
Prosím, uveďte zdroj na opätovné vydaniehttp://blog.csdn.net/z1104222568/article/details/42218125
|