- package com.xjtu.interceptor;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.Interceptor;
- public class PermissionInterceptor implements Interceptor {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- @Override
- public void destroy() {
- // TODO Auto-generated method stub
- }
- @Override
- public void init() {
- // TODO Auto-generated method stub
- }
- @Override
- public String intercept(ActionInvocation invocation) throws Exception {
- System.out.println("进入拦截器");
- Object user = ActionContext.getContext().getSession().get("user");
- System.out.println(user);
- if (user != null) {
- return invocation.invoke();
- }
- ActionContext.getContext().put("message", "您无权执行该操作!");
- return "success";
- }
- }
- package com.xjtu.soft;
- public class HelloWorldAction {
- private String message;
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- public String execute(){
- System.out.println("执行execute");
- message = "execute";
- return "success";
- }
- public String addUI() {
- System.out.println("执行addUI");
- message = "addUI";
- return "success";
- }
- }
struts.xml配置自定义的拦截器如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <constant name="struts.action.extension" value="do,action" />
- <constant name="struts.multipart.maxSize" value="10485760" />
- <package name="itcast" namespace="/test" extends="struts-default">
- <interceptors>
- <interceptor name="permission"
- class="com.xjtu.interceptor.PermissionInterceptor" />
- <interceptor-stack name="permissionstack">
- <interceptor-ref name="defaultStack"></interceptor-ref>
- <interceptor-ref name="permission"></interceptor-ref>
- </interceptor-stack>
- </interceptors>
- <global-results>
- <result name="success">/WEB-INF/page/message.jsp</result>
- </global-results>
- <action name="list_*" class="com.xjtu.soft.HelloWorldAction"
- method="{1}">
- <interceptor-ref name="permissionstack"></interceptor-ref>
- </action>
- </package>
- </struts>