日期:2014-05-17  浏览次数:20660 次

Structs2自定义拦截器
直接上代码
配置文件
<struts>
<include file ="struts-default.xml" /> 
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="permission" class="my.test.aop.PermissionInterceptor"/>
<interceptor-stack name="permissionStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="permission"/>
</interceptor-stack>
</interceptors>
<action name="aop_*" class="my.test.interceptAction" method="{1}">
<interceptor-ref name="permissionStack"/>
<result name="success">msg.jsp</result>
</action>
</package>

</struts>

Action:
public class interceptAction {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String test1(){
this.message = "test1";
return "success";
}
public String execute(){
this.message = "execute";
return "success";
}

}

拦截器:
public class PermissionInterceptor implements Interceptor {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public void destroy() {

}

public void init() {

}

public String intercept(ActionInvocation invocation) throws Exception {  //invocation被拦截的方法
Object user = ActionContext.getContext().getSession().get("user");
if(user != null){
return invocation.invoke();//被拦截方法执行。 方法执行后会返回“success”,在这里方法执行后也要原样的返回这个“success”
}
ActionContext.getContext().put("message", "未登录");
return "message";
}

}


最后总报错No result defined for action my.test.interceptAction and result message
interceptor Structs2

------解决方案--------------------
你的return "message";在配置文件中没有配置message的结果类型,
------解决方案--------------------
求分 - - 我帮你搞定了