0

我遇到了一些问题,<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 和 loadBoard2 一样运行decryptionBean.loadBoard()decryptionBean.readAllBoards()而 loadBoard2 只运行decryptionBean.readAllBoards()。如果我删除该<f:event type="preRenderView" listener="#{decryptionBean.readAllBoards}>行,则 loadBoard1 和 2 正常运行(调用正确/无方法)。关于可能发生的事情有什么想法吗?

提前致谢!

4

1 回答 1

1

我不知道你期望 preRenderView 事件做什么,但它总是会在 RENDER_VIEW 阶段之前被调用(因此得名)。

单击按钮将导致视图重新呈现,因此当您单击按钮时将始终调用该方法。如果您使用 ajax 并且只渲染视图的一小部分,则不会触发该事件。

如果您不想在每次呈现视图时都调用该方法,您可以尝试使用 ViewScoped bean 和 @PostConstruct 方法。

于 2013-07-29T13:32:08.260 回答