Brief use-case description:
简要用例说明:
The user arrives to the page: http://localhost:8080/.../show.xhtml?itemId=1
. My ShowBean
is a @RequestScoped JSF Managed Bean which obtains the id through <f:viewParam.../>
and looks for item in the database:
用户到达页面:http:// localhost:8080 /.../ show.xhtml?itemId = 1。我的ShowBean是一个@RequestScoped JSF Managed Bean,它通过
<f:metadata>
<f:viewParam name="itemId" value="#{showBean.itemId}">
<f:convertNumber integerOnly="#{true}"/>
</f:viewParam>
</f:metadata>
<f:event type="preRenderView" listener="#{showBean.init()}"/>
The user can also edit the displayed item. I want to construct the @ViewScoped ItemEditBean
on-demand (by clicking on the "Edit" button). To achieve this I did the following:
用户还可以编辑显示的项目。我想按需构建@ViewScoped ItemEditBean(通过单击“编辑”按钮)。为此,我做了以下事情:
<p:commandButton value="Edit" oncomplete="editWidget.show()" update=":itemEditForm">
<f:setPropertyActionListener target="#{itemEditBean.id}" value="#{showBean.itemId}"/>
</p:commandButton>
My questions are: Is there any better approach to pass itemId to the @ViewScoped bean?
我的问题是:有没有更好的方法将itemId传递给@ViewScoped bean?
EDIT:
Doing itemEditBean.fetch(id)
in the action of the p:command
button won't initialize the @ViewScoped bean on page rendering.
在p:命令按钮的操作中执行itemEditBean.fetch(id)将不会在页面呈现时初始化@ViewScoped bean。
<p:commandButton value="Edit" oncomplete="editWidget.show()" update=":itemEditForm" action="#{itemEditBean.fetchItem(showBean.itemId)}"/>
.
With the code above itemEditBean gets constructed on-demand.
使用上面的代码,itemEditBean可以按需构建。
2 个解决方案
#1
0
I think @ViewScoped is no pro. Try partial form submit. For example like this ,
我认为@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>
#2
0
the problem is that showEditBean
is re-constructed on INVOKE_APPLICATION
in the subsequent request that does not preserve parameters.
问题是showEditBean在后续请求中的INVOKE_APPLICATION上重新构造,该请求不保留参数。
just make sure to preserve them using f:param
.
只需确保使用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. you were right about @ViewScoped
bean lifecycle: it is NOT constructed on page load if appears only in action
s!
附:你对@ViewScoped bean生命周期是正确的:如果仅出现在操作中,它不会在页面加载上构建!
#1
0
I think @ViewScoped is no pro. Try partial form submit. For example like this ,
我认为@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>
#2
0
the problem is that showEditBean
is re-constructed on INVOKE_APPLICATION
in the subsequent request that does not preserve parameters.
问题是showEditBean在后续请求中的INVOKE_APPLICATION上重新构造,该请求不保留参数。
just make sure to preserve them using f:param
.
只需确保使用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. you were right about @ViewScoped
bean lifecycle: it is NOT constructed on page load if appears only in action
s!
附:你对@ViewScoped bean生命周期是正确的:如果仅出现在操作中,它不会在页面加载上构建!