Jsf 渲染f:viewAction在某些情况下
我有一个Jsf 渲染f:viewAction在某些情况下,jsf,nullpointerexception,jsf-2.2,Jsf,Nullpointerexception,Jsf 2.2,我有一个主页页面: <f:metadata> <f:viewAction action="#{booksBean.selectBook()}"/> </f:metadata> <h:head> <title>home</title> </h:head> <h:body> ... <h:link > <h:graphicImage name="images/
主页
页面:
<f:metadata>
<f:viewAction action="#{booksBean.selectBook()}"/>
</f:metadata>
<h:head>
<title>home</title>
</h:head>
<h:body>
...
<h:link >
<h:graphicImage name="images/books/s1.jpg" />
<f:param name="isbn" value="25413652" />
</h:link>
...
</h:body>
问题出现在前面提到的部分,当用户移动到home.xhtml
时,会自动调用booksBean.selectBook()
,因为它仍然为null,所以我得到了NullPointerException
如何从
login
页面转到home
页面而不调用booksBean.selectBook()
?以下是不使用f:viewaction
<h:commandButton value="optional value" action="#{booksBean.selectBook()}">
<f:param name="isbn" value="25413652"></f:param>
<f:ajax execute="@form" render="@form"></f:ajax>
</h:commandButton>
您可以通过指定
if
条件有选择地调用f:viewAction
。在您的情况下,您似乎只希望在回发时执行操作,在这种情况下,您可以:
<f:viewAction action="#{booksBean.selectBook()}" if="#{facesContext.postBack}"/>
相关的:
h:link
,我怎么能在没有viewAction
的情况下,通过一个指向bean方法的链接发送GET
参数呢?这正好修复了我以前在视图带有“required”(没有设置属性,破坏了第二个AJAX请求)时得到的NPE
未设置参数。例如,/faces/admin/admin\u user\u edit.xhtml?userId=x
与/faces/admin/admin\u user\u edit.xhtml
相比,第二个选项触发了NPE,因为触发了f:viewAction
,但未设置支持bean中所需的属性(因为没有特定的参数)。希望这能帮助更多的人。但现在我的编辑表单是空的:-(
<f:viewAction action="#{booksBean.selectBook()}" if="#{facesContext.postBack}"/>