繁体   English   中英

使用 Seam、RichFaces 和 PopupPanel 传递参数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM