我正在阅读Josh Juneau撰写的Java EE 7书籍“Java EE 7 Recipes”。我试图从第3章(配方3.5)运行一些示例代码来测试条件导航。代码非常简单明了,但不幸的是我的行为很奇怪。
JSF Page脚本如下:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Login Test</title>
</h:head>
<h:body>
<h:form id="resourceTest">
<h1>Login</h1>
<h:commandButton id="loginButton" action="#{loginTest.login}" value="Login" />
</h:form>
</h:body>
</html>
和JSF Managed Bean:
package com.test.controller;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class LoginTest {
private boolean authenticated;
public LoginTest() {
authenticated = false;
}
public boolean isAuthenticated() {
return authenticated;
}
public void setAuthenticated(boolean authenticated) {
this.authenticated = authenticated;
}
public void login(){
setAuthenticated(true);
}
}
faces-config.xml中
<navigation-rule>
<navigation-case>
<from-action>#{loginTest.login}</from-action>
<if>#{loginTest.authenticated}</if>
<to-view-id>/faces/nextpage.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
当我点击“登录”按钮时,它会生成异常参数错误:参数ID为空
Stacktrace如下:
[2013-11-22T10:28:14.893+0000] [glassfish 4.0] [WARNING] [] [javax.enterprise.resource.webcontainer.jsf.lifecycle] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1385116094893] [levelValue: 900] [[
Argument Error: Parameter id is null
java.lang.NullPointerException: Argument Error: Parameter id is null
at com.sun.faces.util.Util.notNull(Util.java:425)
at com.sun.faces.flow.FlowHandlerImpl.getFlow(FlowHandlerImpl.java:89)
.....
.....
at java.lang.Thread.run(Thread.java:724)
]]
[2013-11-22T10:28:14.909+0000] [glassfish 4.0] [FATAL] [jsf.context.exception.handler.log] [javax.enterprise.resource.webcontainer.jsf.context] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1385116094909] [levelValue: 1100] [[
JSF1073: javax.faces.FacesException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=, Message=Argument Error: Parameter id is null]]
[2013-11-22T10:28:14.909+0000] [glassfish 4.0] [FATAL] [] [javax.enterprise.resource.webcontainer.jsf.context] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1385116094909] [levelValue: 1100] [[
Argument Error: Parameter id is null
javax.faces.FacesException: Argument Error: Parameter id is null
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:89)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
..........
at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.NullPointerException: Argument Error: Parameter id is null
at com.sun.faces.util.Util.notNull(Util.java:425)
at com.sun.faces.flow.FlowHandlerImpl.getFlow(FlowHandlerImpl.java:89)
at com.sun.faces.application.NavigationHandlerImpl.determineViewFromActionOutcome(NavigationHandlerImpl.java:1204)
....
]]
我错过了什么吗?
此外,如果我将JSF页面中的EL表达式更改为#{loginTest.login()} (即附加括号),我不会收到任何错误消息,但重定向不会工作。这有什么不同吗?
答案 0 :(得分:7)
我复制了它。这是Mojarra中的一个错误,它与我之前已经报道过的issue 3054具有相同的理由。如果导航案例中没有流文档ID,NavigationCase#getToFlowDocumentId()
将返回一个空字符串,而不是null
,但代码只执行空检查。
我已将此错误报告为issue 3087。该修复有望在2.2.5中。同时,最好的办法是忘记导航案例并使用隐式导航。编辑登录方法如下:
public String login() {
setAuthenticated(true);
return "/faces/page.xhtml?faces-redirect=true";
}
摆脱那个导航案例。