我正在创建一个简单的购物车应用程序。在此,当用户从面板中选择一个项目并单击面板外部的“添加到购物车”按钮时,这些项必须显示在数据网格中。但不幸的是,我无法实现它。 index.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view>
<h:head>
<div id="content">
<ui:insert name="content">
<ui:include src="/resources/templates/template.xhtml" />
</ui:insert>
</div>
</h:head>
<h:body>
<h:form id="index">
<br />
<p:growl id="msgs" showDetail="true" />
<p:tabView orientation="top" id="panel">
<p:tab title="Men" id="men">
<h:panelGrid id="MenPanel">
<ui:include src="/men1.xhtml"></ui:include>
</h:panelGrid>
</p:tab>
<p:tab title="Women" id="women">
<h:panelGrid id="WomenPanel">
<ui:include src="/women1.xhtml"></ui:include>
</h:panelGrid>
</p:tab>
</p:tabView>
<br />
<p:commandButton value="Add to Bag" update="dlg2" process="panel"
oncomplete="PF('dlg2').show()" />
<p:dialog header="Bag" modal="true" showEffect="clip"
widgetVar="dlg2" id="dlg2" resizable="false" width="300"
height="200">
<p:outputPanel id="bag">
<p:dataTable var="item" value="#{itemDataBean.userselectedItems}">
<p:column headerText="Item Id">
<h:outputText value="#{item.itemid}" />
</p:column>
<p:column headerText=" Item Name">
<h:outputText value="#{item.name}" />
</p:column>
<p:column headerText="Item Price">
<h:outputText value="#{item.price}" />
</p:column>
<p:column headerText="Item Price">
<h:outputText value="#{item.size}" />
</p:column>
</p:dataTable>
</p:outputPanel>
</p:dialog>
<p:commandButton value="Checkout" update="dlg2"
oncomplete="PF('dlg').show()" />
<p:dialog header="Cart" modal="true" showEffect="clip"
widgetVar="dlg" resizable="false" width="500" height="500" id="dlg">
<p:outputPanel id="cart">
<p:dataTable var="item" value="#{itemDataBean.cartlist}">
<p:column headerText="Item Id">
<h:outputText value="#{item.itemid}" />
</p:column>
<p:column headerText=" Item Name">
<h:outputText value="#{item.name}" />
</p:column>
<p:column headerText="Quantity">
<h:outputText value="1" />
</p:column>
<p:column headerText="Item Price">
<h:outputText value="#{item.price}" />
</p:column>
<p:columnGroup type="footer">
<p:row>
<p:column colspan="3" style="text-align:right"
footerText="Total Price:" />
<p:column footerText="#{itemData.total}" />
</p:row>
</p:columnGroup>
</p:dataTable>
</p:outputPanel>
</p:dialog>
</h:form>
</h:body>
</f:view>
</html>
men1.xhtml:
<h:form id="MenItemForm" prependId="false">
<p:outputPanel>
<p:outputLabel value="Select Category : ">
<br />
</p:outputLabel>
</p:outputPanel>
<p:selectManyCheckbox id="shopmen" value="#{itemDataBean.selectedMenItems}">
<br />
<f:selectItems value="#{itemDataBean.menItems}" var="item"
itemValue="#{item.itemid}" itemLabel="#{item.name}" />
<br />
</p:selectManyCheckbox>
<br />
<br />
</h:form>
Women1.xhtml:
<h:form id="womenItemForm" prependId="false">
<p:outputPanel>
<p:outputLabel value="Select Category : ">
<br />
</p:outputLabel>
</p:outputPanel>
<p:selectManyCheckbox id="shopwomen" value="#{itemDataBean.selectedWomenItems}">
<br />
<f:selectItems value="#{itemDataBean.womenItems}" var="item"
itemValue="#{item.itemid}" itemLabel="#{item.name}" />
<br />
</p:selectManyCheckbox>
<br />
<br />
</h:form>
和bean类:
/ ** *用于获取UI数据的类 * /
package com.shop.web;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import com.shop.data.Item;
/**
* @author svalsalan
*
*/
@ManagedBean(name = "itemDataBean")
@SessionScoped
public class ItemDataBean {
private List<Item> MenItems = new ArrayList<Item>();
private List<Item> WomenItems = new ArrayList<Item>();
private List<Item> Items = new ArrayList<Item>();
public List<String> selectedItems = new ArrayList<String>();
public List<String> selectedMenItems = new ArrayList<String>();
public List<String> selectedWomenItems = new ArrayList<String>();
public List<Item> userselectedItems = new ArrayList<Item>();
List<Item> Cartlist = new ArrayList<Item>();
public List<Item> getItems() {
Items = new ArrayList<Item>();
// set an initial value for the price
// Create items
Item Shirt = new Item(1, "Shirt", 100, "S");
Shirt.setItemid(1);
Item tshirt = new Item(1, "T-Shirt", 200, "M");
tshirt.setItemid(2);
Item jean = new Item(1, "Jeans", 300, "L");
jean.setItemid(3);
Item Dress = new Item(0, "Dress", 300, "S");
Dress.setItemid(4);
Item Tops = new Item(0, "Tops", 100, "S");
Tops.setItemid(5);
Item skirt = new Item(0, "Skirt", 200, "S");
skirt.setItemid(6);
// Add them to the list
Items.add(0, Shirt);
Items.add(1, tshirt);
Items.add(2, jean);
Items.add(3, Dress);
Items.add(4, Tops);
Items.add(5, skirt);
return Items;
}
/**
* @return the menItems
*/
public List<Item> getMenItems() {
MenItems.clear();
getItems();
for (int i = 0; i < Items.size(); i++) {
if (Items.get(i).getCategoryId() == 1) {
MenItems.add(Items.get(i));
}
}
return MenItems;
}
/**
* @return the womenItems
*/
public List<Item> getWomenItems() {
WomenItems.clear();
getItems();
for (int i = 0; i < Items.size(); i++) {
if (Items.get(i).getCategoryId() != 1) {
WomenItems.add(Items.get(i));
}
}
return WomenItems;
}
/**
* @param menItems
* the menItems to set
*/
public void setMenItems(List<Item> menItems) {
MenItems = menItems;
}
/**
* @param womenItems
* the womenItems to set
*/
public void setWomenItems(List<Item> womenItems) {
WomenItems = womenItems;
}
/**
* @return the items
*/
/**
* @param items
* the items to set
*/
public void setItems(List<Item> items) {
Items = items;
}
public List<String> getSelectedMenItems() {
return selectedMenItems;
}
/**
* @return the selectedWomenItems
*/
public List<String> getSelectedWomenItems() {
return selectedWomenItems;
}
/**
* @param selectedMenItems
* the selectedMenItems to set
*/
public void setSelectedMenItems(List<String> selectedMenItems) {
this.selectedMenItems = selectedMenItems;
System.out.println(selectedMenItems);
}
/**
* @param selectedWomenItems
* the selectedWomenItems to set
*/
public void setSelectedWomenItems(List<String> selectedWomenItems) {
this.selectedWomenItems = selectedWomenItems;
System.out.println(selectedWomenItems);
}
/**
* @return the selectedItems
*/
public List<String> getSelectedItems() {
this.selectedItems.clear();
selectedItems.addAll(getSelectedWomenItems());
selectedItems.addAll(getSelectedMenItems());
return selectedItems;
}
/**
* @param selectedItems
* the selectedItems to set
*/
public void setSelectedItems(List<String> selectedItems) {
this.selectedItems = selectedItems;
}
/**
* @return the userselectedItems
*/
public List<Item> getUserselectedItems() {
this.userselectedItems.clear();
// loop through the selectedItems array
for (String str : selectedItems) {
// each one item in the array, loop throug the list from
// getMenItem()
for (Item item : Items) {
// Match the ids' and store in a different list
if (str.matches(Integer.toString(item.getItemid()))) {
// add the items to the new list
this.userselectedItems.add(item);
}
}
// return the new list
}
return this.userselectedItems;
}
/**
* @param userselectedItems
* the userselectedItems to set
*/
public void setUserselectedItems(List<Item> userselectedItems) {
this.userselectedItems = userselectedItems;
}
/**
* @return the selectedMenItems
*/
/**
* @return the cartlist
*/
public List<Item> getCartlist() {
Cartlist.clear();
Cartlist.addAll(getUserselectedItems());
return Cartlist;
}
/**
* @param cartlist
* the cartlist to set
*/
public void setCartlist(List<Item> cartlist) {
Cartlist = cartlist;
}
/**
* @return the userselectedMenItems
*/
public int getTotal() {
int total = 0;
for (Item totalItemPrice : getCartlist()) {
total += totalItemPrice.getPrice();
}
return total;
}
}
答案 0 :(得分:1)
我注意到的第一件事是你应该避免嵌套表格(见这里:How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?)
对于更新html中特定部分的问题,通常类似于
update=":name_of_containing_form:id_of_element_to_update"