[英]Passing Parameters With Seam, RichFaces & PopupPanel
我正在嘗試使用 Seam 3、RichFaces 4 讓一個小應用程序工作,並在傳遞一些參數時遇到一些麻煩。 我嘗試了很多不同的事情,但我一直在最后的障礙上跌倒。 我正在通過請求參數傳遞一個 customerId。 但是當我在 popupPanel 上點擊 RichFaces commandButton 時,我不再可以使用該 customerId。
我正在設置一個應用程序來管理一些數據。 基本上,您 select 是一個屏幕上的客戶,這會將您帶到另一個包含“存儲庫”的屏幕,然后您可以在其中創建、編輯等。您可以通過 URL 進入第二個存儲庫頁面:
http://localhost:8080/media-manager/repositories.xhtml?customer=12
然后我有一個 bean 拾取這個值:
@Named
@RequestScoped
public class RepositoryBean extends AbstractViewBean<Repository> {
// Various properties etc. here
private Long customerId;
public void init() {
log.info("Customer ID is "+ customerId);
}
}
然后我通過存儲庫頁面上的元數據設置客戶 ID 並調用 init:
<f:metadata>
<f:viewParam name="customer" value="#{repositoryBean.customerId}"/>
<f:event type="preRenderView" listener="#{repositoryBean.init}" />
</f:metadata>
這在開始時效果很好。 我可以使用提供的 ID 為客戶顯示我需要的信息。 但是當我嘗試創建我的 popupPanel 時出現了一些問題。 這是代碼的簡化版本:
<rich:popupPanel id="repositoryModalPanel" modal="true" resizeable="true">
<f:facet name="header">Title</f:facet>
<f:facet name="controls">
<h:outputLink value="#" onclick="#{rich:component('repositoryModalPanel')}.hide(); return false;">X</h:outputLink>
</f:facet>
<h:form id="modalForm" class="modalForm">
<fieldset>
<ul class="layout form">
<li>
<label for="name" class="required">Name:</label>
<h:inputText id="name" value="#{repositoryBean.instance.name}" required="true">
<!-- rich:validator event="blur" / -->
</h:inputText>
<rich:message for="name" errorClass="error errormessage" />
</li>
<li class="last">
<a4j:commandButton id="create" value="Create" action="#{repositoryBean.saveRepository}" rendered="#{empty repositoryBean.instance.id}"/>
<a4j:commandButton id="save" value="Save" action="#{repositoryBean.saveRepository}" rendered="#{not empty repositoryBean.instance.id}"/>
<a4j:commandButton id="cancel" value="Cancel" action="#{repositoryBean.clearInstance}" immediate="true" />
</li>
</ul>
</fieldset>
</h:form>
基本上,每當我點擊保存命令按鈕時,都會調用 init 方法,但不會填充 customerId 成員。 有沒有人知道為什么?
我讀過 viewParam 僅適用於 GET 請求,所以也許這就是問題所在? 但如果是這樣的話 - 其他解決方案是什么? 我看到的很多建議(例如使用@ManagedProperty)似乎不適用於Seam 3。
RepositoryBean
是 RequestScoped,因此將在每個請求上新創建,尤其是在您點擊保存按鈕時。
直截了當的解決方案是將RepositoryBean
提升為 ConversationScoped,並在進入頁面后使其長期運行。
@Named
@ConversationScoped
public class RepositoryBean extends AbstractViewBean<Repository> {
// Various properties etc. here
@In
Conversation conversation
private Long customerId;
public void init() {
log.info("Customer ID is "+ customerId);
conversation.begin();
}
}
最簡單的方法是轉儲preRenderView
並改用 seam 3 view-action。
<f:metadata>
<f:viewParam name="customer" value="#{repositoryBean.customerId}"/>
<s:viewAction action="#{repositoryBean.init}" if="#{conversation.transient}"/>
</f:metadata>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.