Hi all,
I'm working with simple project using JSF is: Brandi's Bagel House. Everything is OK. And now I want to present elements that the user choice like: Bagel, Topping, Coffee... onto Receipt.xhtml. But I don't know how to do because a data type of selectedToppingNames is an Array.
Please help!
Here is my code:
Index.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:f="http://java.sun.com/jsf/core">
<h:head>
<title>Brandi's Bagel House</title>
</h:head>
<h:body>
<h2>Brandi's Bagel House</h2>
<h:form prependId="false">
<table>
<tbody>
<tr>
<td>
<fieldset>
<legend>Bagel</legend>
<h:selectOneRadio id="#{id.BAGEL}" layout="pageDirection" value="#{indexBean.selectedBagelName}"
converter="#{bagelConverter}">
<f:selectItems var="item" value="#{bagelFacade.findAllOrderById()}"
itemLabel="#{item.items}"/>
</h:selectOneRadio>
</fieldset>
</td>
<td>
<fieldset>
<legend>Topping</legend>
<h:selectManyCheckbox id="#{id.TOPPING}" layout="pageDirection" value="#{indexBean.selectedToppingNames}"
converter="#{toppingConverter}">
<f:selectItems var="item" value="#{toppingFacade.findAllOrderById()}"
itemLabel="#{item.items}"/>
</h:selectManyCheckbox>
</fieldset>
</td>
<td>
<fieldset>
<legend>Coffee</legend>
<h:selectOneRadio id="#{id.COFFEE}" layout="pageDirection" value="#{indexBean.selectedCoffeeName}"
converter="#{coffeeConverter}">
<f:selectItems var="item" value="#{coffeeFacade.findAllOrderById()}"
itemLabel="#{item.category}"/>
</h:selectOneRadio>
</fieldset>
</td>
</tr>
</tbody>
</table>
<div class="Button">
<h:commandButton action="#{indexBean.compute()}" value="Compute"/>
<h:commandButton action="#{indexBean.clear()}" value="Clear"/>
</div>
</h:form>
</h:body>
</html>
IndexBean.java:
package web;
import com.sun.xml.rpc.processor.modeler.j2ee.xml.string;
import data.entity.Bagel;
import data.entity.Coffee;
import data.entity.Topping;
import data.service.SalesTaxFacade;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
@Named
@RequestScoped
public class IndexBean {
private Double subtotal;
private Bagel selectedBagelName;
private Topping[] selectedToppingNames; (THIS IS A PROBLEM...)
private Coffee selectedCoffeeName;
private Double tax;
private Double total;
@EJB private SalesTaxFacade salesTaxFacade;
public String compute() {
subtotal = selectedBagelName.getVal() + computeTopping() + selectedCoffeeName.getPrices();
tax = subtotal * salesTaxFacade.find();
total = subtotal + tax;
return "receipt";
}
public void clear() {
selectedBagelName = null;
selectedToppingNames = null;
selectedCoffeeName = null;
}
//----------------------------------------------------------------------------
public Bagel getSelectedBagelName() {
return selectedBagelName;
}
public void setSelectedBagelName(Bagel selectedBagelName) {
this.selectedBagelName = selectedBagelName;
}
public Coffee getSelectedCoffeeName() {
return selectedCoffeeName;
}
public void setSelectedCoffeeName(Coffee selectedCoffeeName) {
this.selectedCoffeeName = selectedCoffeeName;
}
public Double getSubtotal() {
return subtotal;
}
public Double getTax() {
return tax;
}
public Double getTotal() {
return total;
}
public Topping[] getSelectedToppingNames() {
return selectedToppingNames;
}
public void setSelectedToppingNames(Topping[] selectedToppingNames) {
this.selectedToppingNames = selectedToppingNames;
}
private Double computeTopping() {
double acc = 0.0;
for (Topping eachName : selectedToppingNames) {
acc += eachName.getVal();
}
return acc;
}
}
Receipt.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Receipt</title>
</head>
<body>
<h2>Receipt</h2>
<h:panelGrid columns="3" columnClasses="rightAlign, null, null">
Bagel :
<hutputText value="#{indexBean.selectedBagelName.items}"/>
<hutputText value="#{indexBean.selectedBagelName.val}">
<f:convertNumber type="currency"/>
</hutputText>
Topping:
<hutputText value="#{indexBean.selectedToppingNames}"/> ( I HAVE PROBLEM IN HERE........... )
<h:panelGroup/>
Coffee :
<hutputText value="#{indexBean.selectedCoffeeName.category}"/>
<hutputText value="#{indexBean.selectedCoffeeName.prices}">
<f:convertNumber type="currency"/>
</hutputText>
<h:panelGroup/>
Subtotal :
<hutputText value="#{indexBean.subtotal}">
<f:convertNumber type="currency"/>
</hutputText>
<h:panelGroup/>
Tax:
<hutputText value="#{indexBean.tax}">
<f:convertNumber type="currency"/>
</hutputText>
<h:panelGroup/>
Total:
<hutputText value="#{indexBean.total}">
<f:convertNumber type="currency"/>
</hutputText>
</h:panelGrid>
</body>
</html>
This is the Result onto Index.xhtml:
Bagel
White
Whole Wheat
Topping
Cream cheese
Butter
Peach jelly
Blueberry jam
Coffee
Regular Coffee
Decaf Coffee
Cappuccino
This is the Result onto Receipt.xhtml:
Receipt
Bagel : Whole Wheat $1.50
Topping: [Ldata.entity.Topping;@732859 (PROBLEM IN HERE... HOW TO EXPORT EVERY ELEMENTS HAVE SELECTED?)
Coffee : Regular Coffee $1.25
Subtotal : $2.75
Tax: $0.16
Total: $2.92
Thank you... and hope to received posistive feedback from all of you.