java<f:event type=“preRenderView”>使所有按钮调用相同的方法
我遇到了一些问题,<f:event type="preRenderView" listener="#{decryptionBean.readAllBoards}>
导致我的所有按钮(默认类型,而不是type=“button”>;调用同一个bean方法,特别是decryptionBean.readAllBoards
。以下是我认为与代码相关的部分:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
...
<h:body>
<f:view>
<f:event type="preRenderView" listener="#{decryptionBean.readAllBoards}"/>
</f:view>
...
<p:layout>
<p:layoutUnit id="andon_layout--board1" position="center"
size="50%" styleClass="centered">
<h2>Board 1</h2>
<h:form id="andon_layout--board1--displayForm">
<h:outputLabel for="location_list" value="Board List: " />
<h:selectOneMenu id="location_list" value="Loading..."
required="false">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{decryptionBean.locDropDownArray}" />
</h:selectOneMenu>
<br />
<p:commandButton id="loadBoard1" value="Load board" action="#{decryptionBean.loadBoard}"/>
<h:messages id="b1_messages" />
</h:form>
</p:layoutUnit>
<p:layoutUnit id="andon_layout--board2" position="east"
size="50%" styleClass="centered">
<h2>Board 2</h2>
<h:form id="andon_layout--board2--displayForm">
<h:outputLabel for="location_list" value="Board List: " />
<h:selectOneMenu id="location_list" value="Loading..."
required="false">
<f:selectItem itemLabel="Select One" itemValue="#{decryptionBean.display2}" />
<f:selectItems value="#{decryptionBean.locDropDownArray}" />
</h:selectOneMenu>
<br />
<p:commandButton value="Load board" id="loadBoard2" />
</h:form>
</p:layoutUnit>
</p:layout>
...
loadBoard1运行decryptionBean.loadBoard()
,以及decryptionBean.readAllBoards()
,而loadBoard2只运行decryptionBean.readAllBoards()
。如果我删除了<f:event type="preRenderView" listener="#{decryptionBean.readAllBoards}>
行,loadBoard1和LoadBoard2会正确运行(调用正确/否方法)。你知道会发生什么吗
提前谢谢
# 1 楼答案
我不知道您希望preRenderView事件做什么,但它总是在RENDER_VIEW阶段之前被调用(因此得名)
单击按钮将导致视图重新渲染,因此在单击按钮时将始终调用该方法。如果使用ajax并仅渲染视图的一小部分,则不会触发该事件
如果不想在每次呈现视图时调用该方法,可以尝试ViewScope bean和@PostConstruct方法