【问题标题】:ExternalContext redirect leads to IllegalStateExceptionExternalContext 重定向导致 IllegalStateException
【发布时间】:2021-05-18 07:51:07
【问题描述】:

我遇到了来自我们的一位用户的问题。不知何故,用户只需单击一下即可进行多次重定向。这样做会多次调用重定向方法,这会导致 IllegalStateException。我尝试首先检查响应是否已提交,如果未提交响应,则仅调用重定向方法。它有效。只有一个重定向请求而不是全部被发送到浏览器。但我想知道是否可以发送多个重定向语句。提交旧响应后是否可以使用新响应创建新请求?

这是提交重定向的工作检查:

    public static final boolean redirect(String targetPath) {
    try {
        if(!isCommitted()) {
            exContext().redirect(checkAppendContextPath(targetPath));
            return true;
        }else{
            return false;
        }
    } catch (IOException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(String.format(ERROR_REDIRECT, context().getViewRoot().getViewId(), targetPath));
        }
        return false;
    }
}


private static final boolean isCommitted(){
    if(exContext().getResponse() instanceof HttpServletResponse){
        if(((HttpServletResponse) exContext().getResponse()).isCommitted()){
            return true;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

【问题讨论】:

    标签: java jsf servlets


    【解决方案1】:

    不,不能在服务器端发出请求。它们必须来自客户端。

    我很难相信单次点击会触发多个请求,除非涉及到 JavaScript。如果涉及 JavaScript,请检查它是否存在错误。否则,您可以尝试防止在客户端多次单击。如果您使用 PrimeFaces 并且重定向是从 p:commandButton 触发的,您可以尝试使用 PrimeFaces Extensions CommandButtonSingleClickRenderer

    【讨论】:

    • 用户按下的按钮是 h:commandLink。按下时,它会调用一个进行重定向的方法。非常感谢您的回答。
    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 2012-05-09
    • 2014-06-19
    • 1970-01-01
    • 2013-02-10
    • 2017-03-15
    • 2013-05-04
    • 2021-06-13
    相关资源
    最近更新 更多