ONJava.com -- JBoss Seam --> page 3 SeamJBossBean工作
程序员文章站
2022-07-16 22:27:13
...
第 1, 2, 3, 4, 5 页
我们需要确保后退按钮和多标签或窗口不会破坏应用程序,并且能够正常工作。 为此,将引入一个“交谈”,它在执行搜索时开始,在用户为定单付款时结束。 让我们从一个会话 Bean 入手,它将负责处理搜索操作并在结果之间导航:
package com.jboss.dvd.seam;import [...]@Stateful@Name("search")@Conversational(ifNotBegunOutcome="main")@Scope(ScopeType.CONVERSATION)@Interceptors(SeamInterceptor.class)public class SearchAction implements Search, Serializable{ @In(create=true) ShoppingCart cart; @PersistenceContext(unitName="dvd") EntityManager em; private int pageSize = 20; private int currentPage = 0; private boolean hasMore = false; private Category category = null; private String title = null; private String actor = null; @Out(scope=CONVERSATION,required=false) List <Product> searchResults; @Out(scope=CONVERSATION,required=false) Map <Product, Boolean> searchSelections; // Getters and setters for // category, title and actor @Begin(join=true, processDefinition="shopping") public String doSearch() { currentPage=0; updateResults(); return "browse"; } public String nextPage() { if (!isLastPage()) { currentPage++; updateResults(); }return null; } public String prevPage() { if (!isFirstPage()) { currentPage--; updateResults(); }return null; } public boolean isLastPage() { return (searchResults != null) && !hasMore; } public boolean isFirstPage() { return (searchResults != null) && (currentPage == 0); } private void updateResults() { [...] searchResults = [...] searchSelections = [...] } private Query searchQuery(String title, String actor, Category category) { title = (title == null) ? "%" : "%" + title.toLowerCase() + "%"; actor = (actor == null) ? "%" : "%" + actor.toLowerCase() + "%"; if (category == null) { return em.createQuery( "from Product p where lower(p.title) like " + ":title and lower(p.actor) LIKE :actor") .setParameter("title", title) .setParameter("actor", actor); } else { return em.createQuery( "from Product p where lower(p.title) like " + ":title and lower(p.actor) like :actor " + "and p.category = :category") .setParameter("title", title) .setParameter("actor", actor) .setParameter("category", category); } } public String addToCart() { for (Product item: searchResults) { Boolean selected = searchSelections.get(item); if ( selected!=null && selected ) { searchSelections.put(item, false); cart.addProduct(item, 1); } } return "browse"; } @Destroy @Remove public void destroy() {} }
这个有态会话 Bean 被存储在交谈的上下文中,它将存活于交谈中,并在交谈结束时消亡。 购物车是由 Seam 管理的另一会话 Bean 组件,并被注入到 SearchAction
会话 Bean,这样便不会在在建立多个搜索时丢失它。 searchResults
和 searchSelections
对象将存活于当前交谈中;它们占用的内存将在交谈结束时释放。