Struts2拦截器拦截成功,却不出现返回页面

拦截器代码



import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;


import java.util.Map;


/**

 * Created by allme on 2016/5/25.

 */

public class LoginInterceptor extends AbstractInterceptor {


    @Override

    public String intercept(ActionInvocation actionInvocation) throws Exception {

        try{

            Map session = actionInvocation.getInvocationContext().getSession();

            String username = (String) session.get("username");

            String userpwd = (String) session.get("userpwd");

            if("".equals(username)||"".equals(userpwd)||username==null||userpwd==null){

                System.out.println("what "+username);

                System.out.println("what "+userpwd);

                return "login";

            }

            else{

                return actionInvocation.invoke();

            }

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            return null;

        }

    }

}

配置文件


<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

        "http://struts.apache.org/dtds/struts-2.3.dtd">


<struts>

    <package name="register" extends="struts-default">

        <action name="useraction" class="cn.edu.zucc.action.UserAction">

            <result name="showuser">/showuser.jsp</result>

        </action>

    </package>

    <package name="login" extends="struts-default">

        <interceptors>

            <interceptor name="Login" class="cn.edu.zucc.interceptot.LoginInterceptor" />

            <interceptor-stack name="loginstack">

                <interceptor-ref name="defaultStack"></interceptor-ref>

                <interceptor-ref name="Login"></interceptor-ref>

                <interceptor-ref name="timer"></interceptor-ref>

            </interceptor-stack>

        </interceptors>

        




PIPIONE
浏览 446回答 1
1回答

繁星coding

如果已经运行到你的System.out.println("what "+username);部分,那确实证明拦截器已经正确工作了,但最后没有跳到login页面?死马当活马医吧,试试调整下拦截器的使用顺序?<action name="Hello" class="cn.edu.zucc.action.HelloAction">&nbsp; &nbsp; <interceptor-ref name="loginstack"></interceptor-ref>&nbsp; &nbsp; <result name="showuser">/showuser.jsp</result>&nbsp; &nbsp; <result name="login">/login.jsp</result></action>修正:最终原因@allme&nbsp;自己已找到,是finally里return null;的缘故。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java