JSF 2 Navigation Rule redirect to home page if user is logged
当用户进入登录页面并且已经登录时,我正在尝试重定向到主页。这就是我定义规则的方式:
1 2 3 4 5 6 7 8 | <navigation-rule> <from-view-id>/login.xhtml</from-view-id> <navigation-case> <if>#{identity.loggedIn}</if> <to-view-id>/home.xhtml</to-view-id> <redirect /> </navigation-case> </navigation-rule> |
但我得到了下一个异常:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-26) Error de argumento: el par??metro id es nulo: java.lang.NullPointerException: Error de argumento: el par??metro id es nulo at com.sun.faces.util.Util.notNull(Util.java:425) [jsf-impl-2.2.0-jbossorg-2.jar:] at com.sun.faces.flow.FlowHandlerImpl.getFlow(FlowHandlerImpl.java:89) [jsf-impl-2.2.0-jbossorg-2.jar:] at com.sun.faces.application.NavigationHandlerImpl.determineViewFromActionOutcome(NavigationHandlerImpl.java:1204) [jsf-impl-2.2.0-jbossorg-2.jar:] at com.sun.faces.application.NavigationHandlerImpl.findExactMatch(NavigationHandlerImpl.java:568) [jsf-impl-2.2.0-jbossorg-2.jar:] at com.sun.faces.application.NavigationHandlerImpl.getViewId(NavigationHandlerImpl.java:462) [jsf-impl-2.2.0-jbossorg-2.jar:] at com.sun.faces.application.NavigationHandlerImpl.handleNavigation(NavigationHandlerImpl.java:189) [jsf-impl-2.2.0-jbossorg-2.jar:] at com.sun.faces.application.NavigationHandlerImpl.handleNavigation(NavigationHandlerImpl.java:182) [jsf-impl-2.2.0-jbossorg-2.jar:] at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:132) [jsf-impl-2.2.0-jbossorg-2.jar:] at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.2_spec-2.2.0.jar:2.2.0] at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) [jboss-jsf-api_2.2_spec-2.2.0.jar:2.2.0] at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) [jboss-jsf-api_2.2_spec-2.2.0.jar:2.2.0] at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.2.0-jbossorg-2.jar:] at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.2.0-jbossorg-2.jar:] at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) [jsf-impl-2.2.0-jbossorg-2.jar:] at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646) [jboss-jsf-api_2.2_spec-2.2.0.jar:2.2.0] at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:87) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.websockets.jsr.JsrWebSocketFilter.doFilter(JsrWebSocketFilter.java:138) [undertow-websockets-jsr-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:56) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:82) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:115) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:52) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:51) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:55) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:65) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:70) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3] at org.wildfly.extension.undertow.security.SecurityContextCreationHandler.handleRequest(SecurityContextCreationHandler.java:54) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:185) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:172) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:56) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:107) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.server.HttpHandlers.executeRootHandler(HttpHandlers.java:36) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3] at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:629) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.7.0_25] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_25] at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_25] |
重定向被释放,但引发异常。怎么解决?
1 2 3 4 5 6 7 | <navigation-rule> <from-view-id>/login.xhtml</from-view-id> <navigation-case> <from-outcome>logged</from-outcome> <to-view-id>home.xhtml</to-view-id> <redirect /> </navigation-case> |
**
"logged" 是从 loggedIn 方法返回的值
检查源代码后,问题与结果有关:
com.sun.faces.flow.FlowHandlerImpl.getFlow 方法需要 Id 不为空:
1 2 3 4 | public Flow getFlow(FacesContext context, String definingDocumentId, String id) { Util.notNull("context", context); Util.notNull("definingDocumentId", definingDocumentId); Util.notNull("id", id); |
com.sun.faces.application.NavigationHandlerImpl:
1 | result.isFlowEntryFromExplicitRule = null != fh.getFlow(ctx, toFlowDocumentId, outcome); |
所以结果为 null 并且引发了异常。就我而言,我没有结果。
如果从结果中需要,那么在某个地方必须执行一个动作。
为了实现 login.xhtml 页面需要一个 f:viewAction.
现在实现所有这些我请求的用例运行良好。如果声明了 from-action,则 from-outcome 是可选的,反之亦然。
如果用户转到根文件夹(域/应用程序名/)时需要相同的重定向逻辑,唯一要做的就是将欢迎文件添加到 web.xml。
1 | <welcome-file>login.jsf</welcome-file> |
最后这是导航规则声明
1 2 3 4 5 6 7 8 9 10 | <navigation-rule> <from-view-id>/login.xhtml</from-view-id> <navigation-case> <!--optional<from-action>#{bean.loggedInNavigationRuleAction}</from-action>--> <from-outcome>logged</from-outcome> <if>#{identity.loggedIn}</if> <to-view-id>/home.xhtml</to-view-id> <redirect /> </navigation-case> </navigation-rule> |
这必须添加到 login.xhtml
1 2 3 4 | <f:metadata> <f:viewParam name="x" value="#{bean.x}"/> <f:viewAction action="#{bean.loggedInNavigationRuleAction}" phase="APPLY_REQUEST_VALUES"/> </f:metadata> |