简体   繁体   English

Seam&JSF-误解 <h:inputHidden /> ?

[英]Seam & JSF - misunderstanding <h:inputHidden />?

I've written an e-commerce web application using Seam 2.2, JPA, and JSF that, of course, contains product search functionality. 我已经使用Seam 2.2,JPA和JSF编写了一个电子商务Web应用程序,其中当然包含产品搜索功能。 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. 为此,我创建了一个名为SearchForm的类,该类包含用于搜索的各种参数(起始索引,最大结果数,“和”项,“或”项等)。我还进行了Web操作ProductSearchAction使用SearchForm对象从数据库中提取条目。 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. 在我的JSF中,我显示了<h:form />包含的产品列表,带有2个<h:commandLink />链接,用于向前和向后翻页结果。 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. 由于我没有为每个搜索创建对话,因此我试图通过使用<h:inputHidden />隐藏字段将状态传递给ProductSearchActionSearchForm对象。 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() . 我对<h:inputHidden />理解是,它将在SearchForm填充适当的值,然后将其提供给ProductSearchAction.searchProducts() When I view the HTML source I see the hidden parameters being set within the HTML. 当我查看HTML源代码时,我看到在HTML中设置了隐藏参数。 However, when I click "next" or "previous" which take me to the searchProducts() action none of the values are set. 但是,当我单击“下一个”或“上一个”使我进入searchProducts()操作时,未设置任何值。

Am I misunderstanding how <h:inputHidden /> works? 我是否误解了<h:inputHidden />工作方式? 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. 非常感谢您能提供的任何帮助。

Based on your comment it sounds like you are using h:inputHidden correctly, and that the problem must lie in the JSF bean scoping. 根据您的评论,听起来您使用的是h:inputHidden正确,并且问题必须出在JSF bean范围内。

The beans are behaving as if they are request scope. Bean的行为就好像它们是请求范围一样。 When you fire of ah: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. 当您触发ah:commandLink时,页面将重新渲染并将隐藏的输入发布回去,然后在导航结果(“下一个”或“上一个”)转发到另一个页面后,这些发布的值将不可用。

In all likelihood the @Scope(ScopeType.CONVERSATION) is not behaving as you expect it to. @Scope(ScopeType.CONVERSATION)很有可能没有您期望的那样。 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. 我不是Seam专家,但是从文档的快速扫描来看,除非另有说明,否则Seam似乎将每个单独的HTTP请求都视为“对话”。 So that would explain why the values reset when you click the commandLink. 这样就可以解释为什么单击CommandLink时会重置值。 You probably need to demarcate a long-running conversation with the @Begin/@End annotations. 您可能需要使用@ Begin / @ End注释划定长时间的对话

http://seamframework.org/Community/ConversationExample http://seamframework.org/Community/ConversationExample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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