gpt4 book ai didi

java - 使用 Seam、RichFaces 和 PopupPanel 传递参数

转载 作者:行者123 更新时间:2023-12-01 15:48:59 25 4
gpt4 key购买 nike

我正在尝试使用 Seam 3、RichFaces 4 运行一个小应用程序,但在传递一些参数时遇到了一些麻烦。我尝试过很多不同的事情,但我总是在最后的障碍处跌倒。我通过请求参数传递 customerId。但是,当我点击 popupPanel 上的 RichFaces 命令按钮时,该 customerId 将不再可用。

我正在设置一个应用程序来管理一些数据。基本上,您从一个屏幕中选择一个客户,这将带您到另一个包含“存储库”的屏幕,然后您可以在其中创建、编辑等。您可以通过 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 并使用接缝 3 View 操作。

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

关于java - 使用 Seam、RichFaces 和 PopupPanel 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6523385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com