Java ManagedBean 方法可以返回 null 吗?

发布于 2024-12-10 16:48:40 字数 4474 浏览 0 评论 0原文

当我希望用户返回相同页面时,我总是返回null。例如,如果用户请求编辑组,但他无权访问该组,我通常会返回 null 并打印一些访问被拒绝的消息。如果用户允许,我只返回下一页的名称,例如“编辑”。

例如,

@ManagedBean(name = "groupController")
@SessionScoped
public class GroupController {

    public String prepareEdit() {
        if (userIsAllowed()) {
            return "Edit";
        } else {
            JsfUtil.addAccessDeniedMessage("Access denied.");
            return null; // return to the same page
        }
    }

    private boolean userIsAllowed() {
        return false; // for testing purpose
    }
}

当我返回 null 时,同一页面将返回给用户。到目前为止,一切都很好。但问题是,在 2 个请求之后,我总是得到一个 NullPointerException ,如下所示:

警告:StandardWrapperValve[Faces Servlet]:PWC1406: Servlet.service() for servlet Faces Servlet 抛出异常 java.lang.NullPointerException 在com.sun.faces.application.view.StateManagementStrategyImpl $4.invokeContextCallback(StateManagementStrategyImpl.java:289) 在 javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1253) 在javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:672) 在 javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1262) 在javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:672) 在com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:284) 在com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:177) 在com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:131) 在com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:430) 在 com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:143) 在 com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:199) 在 com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 在 com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110) 在 com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 在 javax.faces.webapp.FacesServlet.service(FacesServlet.java:312) 在 org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188) 在 org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641) 在 com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97) 在 com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185) 在 org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233) 在 com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165) 在com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) 在 com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693) 在 com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954) 在 com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170) 在 com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135) 在 com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102) 在 com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88) 在 com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76) 在com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53) 在 com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57) 在com.sun.grizzly.ContextTask.run(ContextTask.java:69) 在 com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330) 在 com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309) 在 java.lang.Thread.run(Thread.java:619)

有谁知道返回 null 以获得相同页面是否正确?我没有找到任何规范。

I have always returned null when I wanted the user to return to the same page. For example, if the user requests to Edit Groups, but he doesn't have access to this, I usually return null and print some access denied message. If the user is allowed, I just return the name of the next page, like "Edit".

For example,

@ManagedBean(name = "groupController")
@SessionScoped
public class GroupController {

    public String prepareEdit() {
        if (userIsAllowed()) {
            return "Edit";
        } else {
            JsfUtil.addAccessDeniedMessage("Access denied.");
            return null; // return to the same page
        }
    }

    private boolean userIsAllowed() {
        return false; // for testing purpose
    }
}

When I return null, the same page is returned to the user. So far, so good. But the problem is that after 2 requests I always get a NullPointerException like this:

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406:
Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
at com.sun.faces.application.view.StateManagementStrategyImpl$4.invokeContextCallback(StateManagementStrategyImpl.java:289)
at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1253)
at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:672)
at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1262)
at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:672)
at com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:284)
at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:177)
at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:131)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:430)
at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:143)
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:199)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:619)

Does anybody know if is correct to return null to get the same page? I didn't find any specification.

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

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

发布评论

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

评论(1

骑趴 2024-12-17 16:48:40

返回 null 完全没问题。

你的问题是由其他原因引起的。我敢打赌,您使用的是非常早期的 Mojarra 2.0 版本,该版本在部分状态保存方面存在一些严重的错误。我建议升级到最新的2.0甚至2.1。您可以在 Mojarra 主页 找到它们。

Returning null is perfectly fine.

Your problem is caused by something else. I bet that you're using a very early Mojarra 2.0 version which has some serious bugs in partial state saving. I suggest to upgrade to the latest 2.0 or even 2.1. You can find them all at Mojarra homepage.

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