我的模板有:页眉,页脚,菜单(左侧),购物车(右侧)和中心内容。我从菜单中选择类别并通过参数发送到查看页面,该页面覆盖模板中的内容。然后我使用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 © <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";
}
}
答案 0 :(得分:0)
确定。我会找到替代解决方案,就像我之前提到的那样。我通过menuitem的action属性(而不是结果属性)更改视图,该属性具有我在托管bean中获取的链接参数(不像之前那样在视图中)。谢谢你的兴趣。