如何在操作方法完成之前提交响应

发布于 2024-12-16 17:11:11 字数 1792 浏览 2 评论 0原文

当调用 JSF 标记的操作方法时,会显示以下错误消息:

PWC3999: Cannot create a session after the response has been committed

错误消息的含义非常简单,但我的问题是响应如何在操作方法完成执行之前已提交。

(此错误只是偶尔发生,而不是每次单击 commandLink 时发生。)

I use
JSF implementation: Mojarra V2.1.3
JSF component library: Primefaces V2.2.1
Server: GlassFish Open Source Edition V3.1.1 (build 12)

编辑: 我在下面附上一些代码,如果这有助于获得解决方案:

标记:

<h:commandLink value="[Log in]"
                               action="#{headerAndFooterTemplateBacking.loginFilter}"
                               disabled="#{sessionScope.pk > 0 ? true : false}"
                               styleClass="#{sessionScope.pk > 0 ? 'disabled' : 'notDisabled'} padding" immediate="true"/>

操作方法:

public void loginFilter() {
           String from = FacesContext.getCurrentInstance().getViewRoot().getViewId();

        int pk = getSessionMap().get("pk") == null ? -1 : (Integer) (getSessionMap().get("pk"));

        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Object selected = request.getParameter("selected");
        if (pk <= 0) {
            if (selected != null) {
                getSessionMap().put("from", from + "?faces-redirect=true&selected=" + selected);
            } else {
                getSessionMap().put("from", from + "?faces-redirect=true");
            }

            getFacesContext().getApplication().getNavigationHandler().
                    handleNavigation(getFacesContext(), null,
                    "/pages/login/login.xhtml?faces-redirect=true");
        }

    }

The following error message is shown when an action method of a <h:commandLink/> JSF tag is called:

PWC3999: Cannot create a session after the response has been committed

The meaning of the error message is very straightforward, but my question is how the response could have been committed before the action method finishes execution.

(This error is only occurring time to time, not every time the commandLink is clicked.)

I use
JSF implementation: Mojarra V2.1.3
JSF component library: Primefaces V2.2.1
Server: GlassFish Open Source Edition V3.1.1 (build 12)

EDIT : I attach some codes bellow, if this assist in getting the solution:

The markup:

<h:commandLink value="[Log in]"
                               action="#{headerAndFooterTemplateBacking.loginFilter}"
                               disabled="#{sessionScope.pk > 0 ? true : false}"
                               styleClass="#{sessionScope.pk > 0 ? 'disabled' : 'notDisabled'} padding" immediate="true"/>

The action method:

public void loginFilter() {
           String from = FacesContext.getCurrentInstance().getViewRoot().getViewId();

        int pk = getSessionMap().get("pk") == null ? -1 : (Integer) (getSessionMap().get("pk"));

        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Object selected = request.getParameter("selected");
        if (pk <= 0) {
            if (selected != null) {
                getSessionMap().put("from", from + "?faces-redirect=true&selected=" + selected);
            } else {
                getSessionMap().put("from", from + "?faces-redirect=true");
            }

            getFacesContext().getApplication().getNavigationHandler().
                    handleNavigation(getFacesContext(), null,
                    "/pages/login/login.xhtml?faces-redirect=true");
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北笙凉宸 2024-12-23 17:11:11

这是 Mojarra 2.1.x 中的一个错误:问题 2215。尽可能推迟会话创建,以避免不必要的会话创建。但是他们没有考虑到在提交响应之前需要创建会话。当页面大于响应的默认缓冲区大小时,响应将自动提交。

您可以在 中找到多种解决方法添加导致 java.lang.IllegalStateException:提交响应后无法创建会话


与具体问题无关:您应该使用会话范围的bean,而不是手动摆弄会话映射。

This is a bug in Mojarra 2.1.x: issue 2215. The session creation is postponed as much as possible to avoid unnecessary session creation. However they didn't take into account that the session needs to be created before the response is committed. The response will be auto-committed whenever the page is larger than the response's default buffer size.

You can find several workarounds in Adding <h:form> causes java.lang.IllegalStateException: Cannot create a session after the response has been committed.


Unrelated to the concrete problem: you should be using a session scoped bean instead of manually fiddling with the session map.

硬不硬你别怂 2024-12-23 17:11:11

如果您调用 ajax(更新)来呈现页面并且该页面已经呈现,则可能会发生这种情况。此外,如果您更新某些内容,该值也会动态变化。

原因可能有很多,所以是的,代码很重要。

This can happen if you are calling ajax (update) to render the page and that page is already rendered. Also if you update something, which value is changing dynamically.

Can be of a lot of reasons, so YES, the code is important.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文