Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Struts2 execAndWait侦听器在等待后未重定向到成功页面_Struts2_Struts Validation_Struts2 Interceptors - Fatal编程技术网

Struts2 execAndWait侦听器在等待后未重定向到成功页面

Struts2 execAndWait侦听器在等待后未重定向到成功页面,struts2,struts-validation,struts2-interceptors,Struts2,Struts Validation,Struts2 Interceptors,我有一个登录屏幕,其中将发生一些用户输入验证,用户将被验证,最后重定向到欢迎屏幕 以下是登录操作的拦截器定义: <package name="default" extends="struts-default" namespace="/"> <interceptors> <interceptor name="myInterceptor" class="com.interceptor.MyInterceptor"&g

我有一个登录屏幕,其中将发生一些用户输入验证,用户将被验证,最后重定向到欢迎屏幕

以下是
登录操作的拦截器定义:

<package name="default" extends="struts-default" namespace="/">
    <interceptors>  
        <interceptor name="myInterceptor" 
            class="com.interceptor.MyInterceptor"></interceptor>

        <interceptor-stack name="newStack">
            <interceptor-ref name="myInterceptor"/>             
            <interceptor-ref name="defaultStack" />
            <interceptor-ref name="execAndWait">
                <param name="delay">100</param>
                <param name="delaySleepInterval">500</param>
            </interceptor-ref>              
         </interceptor-stack> 
    </interceptors>

    <action name="login"
        class="com.action.LoginAction"> 
        <interceptor-ref name="newStack"/>
        <result name="success">common/Welcome.jsp</result>
        <result name="wait">common/wait.jsp</result>
        <result name="error">Login.jsp</result>
        <result name="input">Login.jsp</result>
    </action>
</package>
   if (isUserAuthenticated) {
        // Some background processing for logging purpose           
        return "success";
    } else {            
        addActionError(getText("error.login"));
        return "error";
    }
我对此代码有几个问题:

1) 对于经过身份验证的用户,将显示
wait.jsp
页面,但不会重定向到
Welcome.jsp

2) 对于未经身份验证的用户,我得到以下异常:

java.lang.NullPointerException
at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:361)
at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:208)
at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:123)
at com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:103)
at com.infy.action.LoginAction.execute(LoginAction.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
at org.apache.struts2.interceptor.BackgroundProcess$1.run(BackgroundProcess.java:57)
at java.lang.Thread.run(Thread.java:662)
在操作或应用程序提供的资源中找不到键
“error.login”
,或者您使用了错误的区域设置。这意味着您没有配置
i18n
资源。要解决您的问题,您需要创建
LoginAction.properties
文件并将密钥放入其中

error.login = Error login

如果您使用的是文章中未看到的全局属性文件,请在此处添加此键

NPE
之所以发生,是因为带有
execAndWait
拦截器的操作在单独的线程中运行,并且您正在调用使用
ActionContext
getText
方法
ActionContext
是线程本地的,这意味着ActionContext中存储的值对于每个线程都是唯一的

为了在进程结束后显示成功页面,您需要不时刷新页面。在这种情况下,可以使用
meta http equiv=“refresh”
完成


  • execAndWait
    使操作在新线程中执行
  • 由于
    ActionContext
    ThreadLocal
    ,因此新线程将不会获取存储在
    ActionContext
    的父线程版本中的值。每个线程都有一个唯一版本的
    ActionContext
  • getText()
    在尝试在新线程中执行时将抛出NPE,因为它依赖于
    ActionContext
  • 要解决此问题,需要将父线程
    ActionContext
    复制到
    execAndWait
    线程中。可以通过扩展
    BackgroundProcess
    类,实现
    beforeInvocation()
    afterInvocation()
    方法,扩展
    ExecuteAndWaitInterceptor
    ,实现
    getNewBackgroundProcess()
    方法来实现这一点

    例子
    您正在刷新等待页面吗?您如何知道该用户已通过身份验证?我在struts.xml中添加了此选项,但不起作用:
    仍在获取NPE:(@user182944:正在刷新等待页面吗?@user182944:为了在作业完成后显示成功页面,您必须轮询相同的操作。感谢您的解释。页面刷新工作正常。但我无法删除
    NPE
    :(我在struts.xml中添加了一个全局属性文件,如下所示:
    文件的条目为:
    error.login=error login
    请告诉我如何解决此问题
    NPE
    @user182944:不要使用
    getText
    方法。如果需要本地化文本,可以自己加载资源。此解决方案在strutr中不起作用。)s2.3.24…有新的解决方法吗?
    ActionInvocation
    已经引用了
    ActionContext
    ,因此您可以在
    YourBackgroundProcess
    @IlGala中直接使用它。有什么问题吗?您看到任何异常吗?请在此处发表评论
    <meta http-equiv="refresh" content="5;url=<s:url includeParams="all" />"/> 
    
    public class YourExecAndWaitInterceptor extends ExecuteAndWaitInterceptor {
    
        private static final long serialVersionUID = 1L;
    
    
        /**
         * {@inheritDoc}
         */
        @Override
        protected BackgroundProcess getNewBackgroundProcess(String arg0, ActionInvocation arg1, int arg2) {
            return new YourBackgroundProcess(arg0, arg1, arg2, ActionContext.getContext());
        }
    
    }
    
    
    
    public class YourBackgroundProcess extends BackgroundProcess {
    
        private final ActionContext context;
    
        public YourBackgroundProcess(String threadName, ActionInvocation invocation, int threadPriority, ActionContext context) {
            super(threadName, invocation, threadPriority);
            this.context = context;
         }
    
        /**
         * {@inheritDoc}
         */
        @Override
        protected void beforeInvocation() {
            ActionContext.setContext(context);
        }
    
        /**
         * {@inheritDoc}
         */
       @Override
        protected void afterInvocation() {
            ActionContext.setContext(null);
        }
    
    }