我使用 Seam 2.2、JPA 和 JSF 编写了一个电子商务 Web 应用程序,当然,它包含产品搜索功能。为了实现这一点,我创建了一个名为 SearchForm
的类,其中包含用于搜索的各种参数(起始索引、最大结果数、“和”术语、“或”术语等)。还有一个 Web 操作 - ProductSearchAction
- 它使用 SearchForm
对象从数据库中提取条目。它看起来像这样:
@Name("searchForm")
@AutoCreate
@Scope(ScopeType.CONVERSATION)
public class SearchForm {
private int startIndex = 0;
private int maxResults = 20;
...
}
@Name("productSearchAction")
@AutoCreate
@Scope(ScopeType.CONVERSATION)
public class ProductSearchAction {
@In
private SearchForm searchForm = null;
@Out
private List<Products> products = null;
...
public void searchProducts() {
...
}
...
}
在我的 JSF 中,我显示包含在
中的产品列表,其中包含 2 用于向前和向后翻页结果的链接。由于我没有为每个搜索创建对话,因此我尝试通过使用 ProductSearchAction
和 SearchForm
对象: inputHidden /> 隐藏字段。我的页面中有这样的字段:
<h:form>
...
<h:inputHidden value="#{searchForm.maxResults}" />
<h:inputHidden value="#{searchForm.startIndex}" />
<h:inputHidden value="#{searchForm.andTerms}" />
...
<h:commandLink action="next" value="Next" />
<h:commandLink action="previous" value="Previous" />
</h:form>
我对
的理解是,它将填充 SearchForm
中的适当值,然后可供 ProductSearchAction.searchProducts()
使用。当我查看 HTML 源代码时,我看到 HTML 中设置了隐藏参数。但是,当我单击“下一个”或“上一个”将我带到 searchProducts()
操作时,没有设置任何值。
我是否误解了
的工作原理?我需要做什么才能将这些值传递给我的搜索操作?有更好的方法来实现我的目标吗?这是Seam Scope的问题吗?我真的很感激你能提供的任何帮助。
I've written an e-commerce web application using Seam 2.2, JPA, and JSF that, of course, contains product search functionality. To accomplish this, I've created a class called SearchForm
that contains the various parameters used for searching (start index, maximum number of results, 'and' terms, 'or' terms, etc.) I've also got a web action -- ProductSearchAction
-- that uses the SearchForm
object to pull the entries from the database. It looks something like this:
@Name("searchForm")
@AutoCreate
@Scope(ScopeType.CONVERSATION)
public class SearchForm {
private int startIndex = 0;
private int maxResults = 20;
...
}
@Name("productSearchAction")
@AutoCreate
@Scope(ScopeType.CONVERSATION)
public class ProductSearchAction {
@In
private SearchForm searchForm = null;
@Out
private List<Products> products = null;
...
public void searchProducts() {
...
}
...
}
In my JSF, I display the list of products enclosed within an <h:form />
, with 2 <h:commandLink />
links for paging forward and backward through the results. Since I don't create a conversation for each search, I'm trying to pass state to the ProductSearchAction
and SearchForm
objects through the use of <h:inputHidden />
hidden fields. I've got fields like this in my page:
<h:form>
...
<h:inputHidden value="#{searchForm.maxResults}" />
<h:inputHidden value="#{searchForm.startIndex}" />
<h:inputHidden value="#{searchForm.andTerms}" />
...
<h:commandLink action="next" value="Next" />
<h:commandLink action="previous" value="Previous" />
</h:form>
My understanding of <h:inputHidden />
is that it will populate the appropriate values within SearchForm
, which will then be made available to ProductSearchAction.searchProducts()
. When I view the HTML source I see the hidden parameters being set within the HTML. However, when I click "next" or "previous" which take me to the searchProducts()
action none of the values are set.
Am I misunderstanding how <h:inputHidden />
works? What do I need to do to pass these values to my search action? Is there a better way to accomplish my goal? Is it a Seam Scope issue? I'd REALLY appreciate any help you can give.
发布评论
评论(1)
根据您的评论,听起来您正在正确使用 h:inputHidden,并且问题必须出在 JSF bean 范围内。
这些 bean 的行为就好像它们在请求范围内一样。当您触发 ah:commandLink 时,页面会重新呈现并将隐藏的输入发回,然后在导航结果(“下一个”或“上一个”)转发到另一个页面后,这些发布的值将不可用。
@Scope(ScopeType.CONVERSATION) 的行为很可能与您期望的不一样。我不是 Seam 专家,但从快速浏览文档来看,除非另有说明,Seam 将每个单独的 HTTP 请求视为一个“会话”。这样就可以解释为什么当您单击命令链接时这些值会重置。您可能需要使用 @Begin/@End 注释来划分长时间运行的对话。
http://seamframework.org/Community/ConversationExample
Based on your comment it sounds like you are using h:inputHidden correctly, and that the problem must lie in the JSF bean scoping.
The beans are behaving as if they are request scope. When you fire of a h:commandLink, the page re-renders and posts the hidden inputs back, and then those posted values are not available after the navigation result ("next" or "prev") forwards to another page.
In all likelihood the @Scope(ScopeType.CONVERSATION) is not behaving as you expect it to. I am not a Seam expert, but from a quick scan of the documentation it looks like Seam treats each individual HTTP request as a "conversation" unless otherwise indicated. So that would explain why the values reset when you click the commandLink. You probably need to demarcate a long-running conversation with the @Begin/@End annotations.
http://seamframework.org/Community/ConversationExample