拦截器会拦截servlet吗
1个回答
2015-05-29 · 知道合伙人互联网行家
关注
展开全部
你好,这个是肯定的呀!下面是截拦的方法!
abstractInterceptor这个是xwork里面的包才有的类,就是你要在struts里面也是要用它。
import java.util.Map;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.ValidationAware;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class AuthorizationInterceptor extends AbstractInterceptor {private static final String USER_KEY = "user";public String intercept(ActionInvocation invocation) throws Exception {Map session = invocation.getInvocationContext().getSession();if(session.get(USER_KEY) == null) {addActionError(invocation, "You must be authenticated to access this page");return Action.ERROR;}return invocation.invoke();}private void addActionError(ActionInvocation invocation, String message) {Object action = invocation.getAction();if(action instanceof ValidationAware) {((ValidationAware) action).addActionError(message);}}}
abstractInterceptor这个是xwork里面的包才有的类,就是你要在struts里面也是要用它。
import java.util.Map;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.ValidationAware;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class AuthorizationInterceptor extends AbstractInterceptor {private static final String USER_KEY = "user";public String intercept(ActionInvocation invocation) throws Exception {Map session = invocation.getInvocationContext().getSession();if(session.get(USER_KEY) == null) {addActionError(invocation, "You must be authenticated to access this page");return Action.ERROR;}return invocation.invoke();}private void addActionError(ActionInvocation invocation, String message) {Object action = invocation.getAction();if(action instanceof ValidationAware) {((ValidationAware) action).addActionError(message);}}}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询