简体   繁体   English

使用 Seam、RichFaces 和 PopupPanel 传递参数

[英]Passing Parameters With Seam, RichFaces & PopupPanel

I'm trying to get a little application working using Seam 3, RichFaces 4 and hitting a few troubles with passing some parameters around.我正在尝试使用 Seam 3、RichFaces 4 让一个小应用程序工作,并在传递一些参数时遇到一些麻烦。 I've tried a lot of different things but I keep falling at the final hurdle.我尝试了很多不同的事情,但我一直在最后的障碍上跌倒。 I'm carrying through a customerId via a request parameter.我正在通过请求参数传递一个 customerId。 But when I hit a RichFaces commandButton on a popupPanel, that customerId is no longer available to me.但是当我在 popupPanel 上点击 RichFaces commandButton 时,我不再可以使用该 customerId。

I'm setting up an application to manage some data.我正在设置一个应用程序来管理一些数据。 Basically you select a customer from one screen, that will take you to another screen containing "repositories" where you then can create, edit etc. You can get to this second repositories page via the URL:基本上,您 select 是一个屏幕上的客户,这会将您带到另一个包含“存储库”的屏幕,然后您可以在其中创建、编辑等。您可以通过 URL 进入第二个存储库页面:

http://localhost:8080/media-manager/repositories.xhtml?customer=12 http://localhost:8080/media-manager/repositories.xhtml?customer=12

I then have a bean picking this value up:然后我有一个 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);
    }
}

I then set the customer ID via metadata on the repository page and call init:然后我通过存储库页面上的元数据设置客户 ID 并调用 init:

<f:metadata>
  <f:viewParam name="customer" value="#{repositoryBean.customerId}"/>
  <f:event type="preRenderView" listener="#{repositoryBean.init}" />
</f:metadata>

This works well at the start.这在开始时效果很好。 I can display information I need for the customer with the supplied ID.我可以使用提供的 ID 为客户显示我需要的信息。 But when I try to create my popupPanel is goes a bit wrong.但是当我尝试创建我的 popupPanel 时出现了一些问题。 Here's a simplified version of the code first:这是代码的简化版本:

<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>

Basically, whenever I hit the save commandButton, the init method is called but the customerId member is never populated.基本上,每当我点击保存命令按钮时,都会调用 init 方法,但不会填充 customerId 成员。 Does anyone have any insight into why?有没有人知道为什么?

I've read that viewParam is only for GET requests so maybe that's the problem?我读过 viewParam 仅适用于 GET 请求,所以也许这就是问题所在? But if that's the case - what is the other solution?但如果是这样的话 - 其他解决方案是什么? Lots of things I have seen suggested (eg using @ManagedProperty) does not seem to be applicable to Seam 3.我看到的很多建议(例如使用@ManagedProperty)似乎不适用于Seam 3。

RepositoryBean is RequestScoped, and as such will be newly created on each request, especially if you hit the save button. RepositoryBean是 RequestScoped,因此将在每个请求上新创建,尤其是在您点击保存按钮时。

The straightforward solution is to promote RepositoryBean to be ConversationScoped, and to make it long running if you enter the page.直截了当的解决方案是将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();
    }
}

The easieast way for that is to dump the preRenderView and use seam 3 view-action instead.最简单的方法是转储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