拦截器(Interceptor)中的invocation.invoke()是什么意思?

关于在Struts2的自定义的验证拦截器(Interceptor)中的invocation.invoke()是什么意思?


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>    



最佳答案:
invocation.invoke() 就是通知struts2接着干下面的事情
比如 调用下一个拦截器 或 执行下一个Action
就等于退出了你自己编写的这个interceptor了

如何使用struts2拦截器,或者自定义拦截器。特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈defaultStack,如下(这里我是引用了struts2自带的checkbox拦截器):

<interceptor-ref name="checkbox">
  <param name="uncheckedValue">0</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
(必须加,否则出错)

你可能感兴趣的:(xml,struts,Interceptor,invoke,invocation)