简体   繁体   中英

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. 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. But when I hit a RichFaces commandButton on a popupPanel, that customerId is no longer available to me.

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:

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

I then have a bean picking this value up:

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

<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. But when I try to create my popupPanel is goes a bit wrong. 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. Does anyone have any insight into why?

I've read that viewParam is only for GET requests so maybe that's the problem? 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.

RepositoryBean is RequestScoped, and as such will be newly created on each request, especially if you hit the save button.

The straightforward solution is to promote RepositoryBean to be ConversationScoped, and to make it long running if you enter the page.

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

<f:metadata>
  <f:viewParam name="customer" value="#{repositoryBean.customerId}"/>
  <s:viewAction action="#{repositoryBean.init}" if="#{conversation.transient}"/>
</f:metadata>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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