我正在读Josh Juneau的Java EE 7书《Java EE 7食谱》。我正在尝试运行第3章(配方3.5)中的一些示例代码来测试条件导航。代码非常简单明了,但不幸的是,我的行为非常奇怪。
JSF页面脚本如下:
<?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托管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为null
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()}(即附加括号),我不会收到任何错误消息,但重定向不起作用。这有什么不同吗?
我复制了它。这是Mojarra中的一个错误,与我之前报告的3054问题有相同的原因。如果导航案例中没有流文档ID,则NavigationCase#getToFlowDocumentId()
返回一个空字符串,而不是null
,但代码仅执行空检查。
我已经将此错误报告为第3087期。修复有望在2.2.5中。与此同时,最好的办法是忘记导航案例并使用隐式导航。按如下方式编辑登录方法:
public String login() {
setAuthenticated(true);
return "/faces/page.xhtml?faces-redirect=true";
}
把导航箱扔掉。