日期:2014-05-20  浏览次数:20704 次

Struts2拦截器求助
第一次使用Struts2,用到了拦截器
我的xml:
<package name="default" namespace="/" extends="json-default">
<interceptors>
<!-- 定义权限检查拦截器 -->
<interceptor name="authority" class="com.yss.alm.Interceptor.AuthorityInterceptor" />
<!-- 定义一个包含权限检查的拦截器栈 -->
<interceptor-stack name="Mydefault">
<!-- 定义拦截器栈包含defaultStack拦截器 -->
<interceptor-ref name="defaultStack" />
<!-- 定义拦截器栈包含authority拦截器 -->
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>

<default-interceptor-ref name="Mydefault" />

<!-- 定义全局Result --> 
<global-results> 
<!-- 当返回login视图名时,转入/login.html页面 --> 
<result name="login">/sylqv.jsp</result>
</global-results>
   
<action name="login" class="YHBAction" method="execute">
<result name="success" type="json">/main.html</result>
<interceptor-ref name="paramsPrepareParamsStack" /> 
</action>
</package>

我的java:

public class AuthorityInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -6486163537439978414L;

// 拦截Action处理的拦截方法
@SuppressWarnings("rawtypes")
public String intercept(ActionInvocation invocation) throws Exception {
// 取得请求相关的ActionContext实例
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();

// 如果没有登陆,返回重新登陆
if (session.get("user") != null) {
System.out.println("Interceptor");
System.out.println("登陆");
return invocation.invoke();

}
// 没有登陆,将服务器提示设置成一个HttpServletRequest属性
ctx.put("tip", "您还没有登陆,请登陆系统");
// 直接返回login的逻辑视图
System.out.println("没有登陆");
return Action.LOGIN;
}
}

我在URl中直接输入登录后的地址时,通过输出调试信息表明拦截器确实起作用了,可是没有自动跳转到login.html画面,只是画面的url还是登录后的url,没有变成 login.html,画面显示空白画面。

求哪位大侠解答一下我哪里出了问题。谢谢

------解决方案--------------------
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/login.html页面 -->
<result name="login">/sylqv.jsp</result>
</global-results>



------解决方案--------------------
应该到global-results定义的页面。
------解决方案--------------------
[Quote:]
<!-- 当返回login视图名时,转入/login.html页面 -->
<result name="login">/sylqv.jsp</result>
</global-results>
[/Quote]

这里定义的明明是 返回login时,转入/sylqv.jsp页面啊。。。。当然不会到login.html页面了
------解决方案--------------------
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/login.html页面 -->
<result name="login">/login.html</result>
 </global-results>
 
------解决方案--------------------
拦截器本身没有问题 白色的页面啊 没找到对应的页面吧 页面的路径?有没有输出错误信息?
------解决方案--------------------
加断点看看进到拦截器内没,如果进入了,就着重看一下返回路径问题
------解决方案--------------------
<result name="success" type="json">/main.html</result>
这个type="json" 我还真没用过,你看下去掉会不会跳到main.html