【问题标题】:JSF Template - Refreshing template components when changing viewJSF 模板 - 更改视图时刷新模板组件
【发布时间】:2013-11-20 08:05:39
【问题描述】:

我的模板有:页眉、页脚、菜单(左侧)、购物车(右侧)和居中的内容。我从菜单中选择类别并通过参数将其发送到覆盖模板内容的查看页面。然后我使用 commandButton 将任何产品添加到购物车。它有效,但接下来我从菜单视图页面更新中选择其他产品的不同类别,但我的购物车(包含在模板中)没有。当我在此类别的查看页面上时,它仍然保持原样。我该如何克服这个问题?感谢您的任何解决方案。

模板:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:metadata>
    <f:viewParam name="category"/>
</f:metadata>
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <h:outputStylesheet name="./css/default.css"/>
    <h:outputStylesheet name="./css/cssLayout.css"/>
    <title>Drummers Store</title>
</h:head>

<h:body>
    <div id="container">

        <div id="top">
            <div id="top-content">
                <div style="text-align: left; margin-left: 50px; margin-top: 20px;">
                    <a href="./index.xhtml"><p:graphicImage   value="./resources/images/logo.png"    height="80px" width="80px" /></a>
                </div>
                <h:form>
                    <p:menuButton style="font-size: 10px;" value="account_name">
                        <p:menuitem value="Account Settings" />
                        <p:separator />
                        <p:menuitem value="Logut" />
                    </p:menuButton>
                </h:form>    
            </div>
        </div>

        <div id="left">
            <div id="left-content">
                <h:form id="form" style="width: 290px;" >
                    <p:panelMenu id="categories" style="text-align: center;">
                    <c:forEach items="#{dSServerBean.categories}" var="c">
                        <p:submenu label="#{c.category_name}">
                            <c:forEach items="#{c.sub_categories}" var="s">
                                <c:if test="#{not s.sub_categories.isEmpty()}">
                                    <p:submenu label="#{s.subCategory_name}">
                                        <c:forEach items="#{s.sub_categories}" var="si">
                                            <p:menuitem value="#{si.subCategory_name}"    outcome="productView">
                                                <f:param name="category" 
                                                         value="#{si.subCategory_name.toLowerCase()}" />
                                            </p:menuitem>
                                        </c:forEach>
                                    </p:submenu>                   
                                </c:if>
                                <c:if test="#{s.sub_categories.isEmpty()}"> 
                                    <p:menuitem value="#{s.subCategory_name}" outcome="productView">
                                        <f:param name="category" 
                                                         value="#{s.subCategory_name.toLowerCase()}" />
                                    </p:menuitem>
                                </c:if>
                            </c:forEach>
                        </p:submenu>
                    </c:forEach>
                </p:panelMenu>
            </h:form>
            </div>
        </div>

        <div id="right">
            <div id="right-content">
                <h:form style="width: 85%; margin: 0 auto; margin-top: 50px;">
                    <p:panel>
                        <f:facet name="header">Cart</f:facet>
                        <f:facet name="footer">To pay: 0 $</f:facet>  
                        <p:scrollPanel id="scroll2"
                                       style="height: 200px; 
                                       max-height: 200px; 
                                       overflow-wrap: break-word;">
                        <p:dataGrid value="#{dSBean.cart}" var="p" 
                                    columns="1" emptyMessage="No products added..."
                                    style="border: 0;">
                                <h:panelGrid>
                                    <h:outputText style="font-size: 10px;" value="#{p.name}" />
                                </h:panelGrid>
                        </p:dataGrid>
                        </p:scrollPanel>
                    </p:panel>
                </h:form>
            </div>
        </div>

        <div id="center">
            <div id="center-content">
                <ui:insert name="content" />
            </div>
        </div>

        <div id="bottom">
            <div id="bottom-content">
                <p style="padding: 1px;">Copyright &copy; <b>Drummers Store</b> 2013</p>
            </div>
        </div>

    </div>

</h:body>

</html>

查看:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/core"
  xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
  xmlns:p="http://primefaces.org/ui">
<f:metadata>
    <f:viewParam name="category"/>
</f:metadata>
<body>
    <ui:composition template="./templates/mainLayout.xhtml">
        <ui:define name="right">
            right
        </ui:define>

        <ui:define name="content">
            <h:form>  
                <p:scrollPanel id="scroll1" style="height: 700px; max-height: 700px; width: 100%;">
                    <p:dataGrid value="#{dSServerBean.getProducts(param.category)}" 
                                var="product" columns="1">
                        <p:panel header="#{product.name}">
                            <h:panelGrid style="width:100%;">
                                <h:outputText value="#{product.description}" />

                                <h:commandButton value="DODAJ" action="#{dSBean.addToCart(product)}" >
                                    <f:param name="category" value="#{param.category}" />
                                </h:commandButton>

                            </h:panelGrid>
                        </p:panel>
                    </p:dataGrid>              
                </p:scrollPanel>
            </h:form>

        </ui:define>
    </ui:composition>

</body>
</html>

DSServerBean:

@ManagedBean
@ApplicationScoped
public class DSServerBean {

public Set<Category> categories = new CategoriesTemplate().categoriesTemplate;
public static Set<Product> products = new ProductsTemplate().productsTemplate;
public Map<String, Set<Product>> category_products;

public DSServerBean() {
    category_products = new TreeMap<>();

    for(Category category : categories){
        category_products.put(category.getCategory_name().toLowerCase(), new TreeSet<Product>());
        for(SubCategory subCategory : category.getSub_categories()){
            category_products.put(subCategory.getSubCategory_name().toLowerCase(), new TreeSet<Product>());
            for(SubCategory subSubCategory : subCategory.getSub_categories()){
                category_products.put(subSubCategory.getSubCategory_name().toLowerCase(), new TreeSet<Product>());
            }
        }
    }

    for(Product product : products){
        for(String category : product.getCategories().split(",")){
            category_products.get(category).add(product);
        }
    }
}

public Set<Category> getCategories() {
    return categories;
}

public Map<String, Set<Product>> getCategory_products() {
    return category_products;
}

public List<Product> getProducts(String category){
    List<Product> view = new ArrayList<>();
    for(Product p : category_products.get(category))
        view.add(p);
    return view;
}
}

DSBean:

@ManagedBean
@SessionScoped
public class DSBean {

private User logged_user;
private List<Product> cart;

public DSBean(){
    logged_user = new User("Marian");
    cart = new ArrayList<>();
}

public User getLogged_user() {
    return logged_user;
}

public List<Product> getCart() {
    return cart;
}

    //METHODS

public String addToCart(String product){
    System.out.println(product);
    for(Product p : DSServerBean.products)
        if(p.toString().equals(product))
            cart.add(p);
    return "productView";
}


}

【问题讨论】:

  • 不显示dSServerBean 代码,很难说问题出在哪里。请出示 dSServerBean 的代码。
  • DsServerBean 具有带类别的树集,以及树图>。目前它仅用于返回包含给定类别产品的数组列表。其应用范围。我回家后肯定会发布它的代码。
  • 您在模板和视图中没有双 &lt;body&gt; 标签吗?然而,视图的唯一可见部分是 &lt;ui:define name="content"&gt;
  • 一个思路:将购物篮页面的内容包裹在一个表单中,并在commandButton的update标签中添加“:basketForm”通过ajax重绘购物篮。然而@Masud 是对的 - 如果没有支持 bean 代码,很难看到你做了什么以及你出了什么问题。
  • @7SpecialGems 他总是可以update="@all"

标签: jsf templates view primefaces facelets


【解决方案1】:

好的。正如我之前提到的,我会找到替代解决方案。我通过 menuitem 的 action 属性(而不是结果属性)更改视图,该属性具有我在托管 bean 中获得的链接参数(不像以前那样在视图中)。感谢您的兴趣。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-05
    • 2023-04-10
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    相关资源
    最近更新 更多