2

I am creating a web page that get an id from a previous page using getSessionMap() in FacesUtils. If the page did not get the id, it should be redirected to other page, in this case draft.jsf. Here is the code:

if ((FacesUtils.getSessionMap().get("id") == null)) {
    try {
        FacesUtils.getResponse().sendRedirect("draft.jsf");
        return;         
    } catch (Exception e) {
        e.printStackTrace();
}
} else {
    id = Long.valueOf(String.valueOf(FacesUtils.getSessionMap().get("id")));
}

But I always get java.lang.IllegalStateException: Committed when the id was null, and the page was not redirected to draft.jsf. What is wrong with my code? Any help would be really appreciated.

Here is the stacktrace:

java.lang.IllegalStateException: Committed
at org.mortbay.jetty.Response.resetBuffer(Response.java:1023)
at org.mortbay.jetty.Response.sendRedirect(Response.java:428)
at org.jleaf.erp.inv.controller.EditAssemblyController.init(EditAssemblyController.java:58)
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 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:340)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:293)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:130)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:393)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1415)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:518)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:331)
at org.jlego.web.jsf.ViewScope.get(ViewScope.java:20)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:327)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
at org.jlego.baseweb.PieceletELResolver.getValue(PieceletELResolver.java:65)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:99)
at com.sun.el.parser.AstValue.getValue(AstValue.java:158)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:69)
at org.primefaces.component.inputtext.InputTextRenderer.encodeMarkup(InputTextRenderer.java:79)
at org.primefaces.component.inputtext.InputTextRenderer.encodeEnd(InputTextRenderer.java:50)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:55)
at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:43)
at org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:191)
at org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:114)
at org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:55)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221)
at org.jlego.web.WebLoginSessionFilter.doFilter(WebLoginSessionFilter.java:95)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
4

2 回答 2

15

这可能是因为您通过普通 servletHttpServletResponse#sendRedirect()而不是 JSF 自己的重定向,ExternalContext#redirect()后者在之后隐式执行FacesContext#responseComplete()调用,指示 JSF 不执行渲染响应,这将用目标视图的输出填充 HTTP 响应。

例如

public void someMethod() throws IOException {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    Long id = (Long) ec.getSessionMap().get("id");

    if (id == null) {
        ec.redirect("draft.jsf");
        return;
    }

    // ... ?
}

(请注意,我还在您的代码中简化了一些笨拙(按摩长<-->字符串)和粗心(吞咽异常);使用@ManagedProperty("#{id}")注入属性毕竟可能更容易)

每当您从 JSF 工件内部导入某些东西时javax.servlet,您应该停止开发并在课堂上环顾四周,ExternalContext并三思而后行,如果您真的以正确的方式做事。此外,JSF 实用程序库OmniFaces有一个方便的Faces实用程序类,无需重新发明FacesUtils轮子。


更新:鉴于堆栈跟踪,另一个可能的原因是您试图在呈现响应的过程中发送重定向。无法从客户端取回已呈现响应的已发送字节以更改响应。您应该在响应的第一个字节发送到客户端之前执行重定向作业。例如,在预渲染视图侦听器方法中,而不是 bean 的(后)构造函数甚至 getter 方法。

<f:event type="preRenderView" listener="#{bean.someMethod}" />
于 2013-05-28T15:11:52.650 回答
0

在我的代码中的其他地方在同一个请求中发送重定向之前,我遇到了这个问题。

仔细检查您是否也没有任何过滤器尝试进行重定向。

于 2013-05-28T15:11:38.020 回答