简要用例说明:
用户到达页面:http://localhost:8080/.../show.xhtml?itemId=1
。
我的ShowBean
是一个@RequestScoped JSF Managed Bean,它通过<f:viewParam.../>
获取id并在数据库中查找项目:
<f:metadata>
<f:viewParam name="itemId" value="#{showBean.itemId}">
<f:convertNumber integerOnly="#{true}"/>
</f:viewParam>
</f:metadata>
<f:event type="preRenderView" listener="#{showBean.init()}"/>
用户还可以编辑显示的项目。我想按需构建@ViewScoped ItemEditBean
(通过单击&#34;编辑&#34;按钮)。为此,我做了以下几点:
<p:commandButton value="Edit" oncomplete="editWidget.show()" update=":itemEditForm">
<f:setPropertyActionListener target="#{itemEditBean.id}" value="#{showBean.itemId}"/>
</p:commandButton>
我的问题是:有没有更好的方法将itemId传递给@ViewScoped bean?
修改
在itemEditBean.fetch(id)
按钮的操作中执行p:command
不会在页面渲染时初始化@ViewScoped bean。
<p:commandButton value="Edit" oncomplete="editWidget.show()" update=":itemEditForm" action="#{itemEditBean.fetchItem(showBean.itemId)}"/>
。
使用上面的代码,itemEditBean按需构建。
答案 0 :(得分:0)
我认为@ViewScoped不是专业人士。 尝试部分表单提交。 例如,像这样,
<p:commandLink immediate="true" id="addAccessoriesLink" update=":accessoriesEntryForm">
<h:graphicImage url="/images/add.png" styleClass="action-img"/>
<f:setPropertyActionListener value="# AccessoriesActionBean.newAccessories}" target="#{AccessoriesActionBean.newAccessories}" />
</p:commandLink>
答案 1 :(得分:0)
问题是showEditBean
在后续请求中的INVOKE_APPLICATION
上重新构建,不保留参数。
只需确保使用f:param
保留它们。
<p:commandButton value="Edit" oncomplete="editWidget.show()" update=":itemEditForm">
<f:param name="itemId" value="#{showBean.itemId}"/>
<f:setPropertyActionListener target="#{itemEditBean.id}" value="#{showBean.itemId}"/>
</p:commandButton>
P.S。你对@ViewScoped
bean生命周期是正确的:如果仅在action
s中出现,那么在页面加载时它是 NOT !