Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How can I export elements in array to .xhtml

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I export elements in array to .xhtml

    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.


  2. #2
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I export elements in array to .xhtml

    Here's what is BASICALLY the problem:
    You're having topping point to the array itself, not the items inside of it. Instead of calling indexBean.selectedToppingNames try making a method within your java code to get all of the items in the topping array. Here is an example:

    public String getToppingNames(){
    String toppingNames = "";
    for(int i = 0; i < selectedToppingNames.length(); i++){
    toppingNames+=selectedToppingNames[i]+"\n";
    }

    }

    Hope this helps!

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I export elements in array to .xhtml

    Thank you upfish... From your ideas I think my problem has solved. I'm try making a method getToppingNames() to get items and value from an array.

    Here is my code in IndexBean.java:
    ........
    ........
    public void setSelectedToppingNames(Topping[] selectedToppingNames) {
    this.selectedToppingNames = selectedToppingNames;
    }

    private double computeTopping() {
    double acc = 0.0;
    for (Topping eachName : selectedToppingNames) {
    acc += eachName.getVal();
    }
    return acc;
    }

    public String getToppingNames() {
    String toppingNames = "";
    for (Topping eachName : selectedToppingNames) {
    toppingNames += eachName.getItems() + "\n" + eachName.getVal() + ";" + "\n" + "\n";
    }
    return toppingNames;
    }

    }

    And... in receipt.xhtml:
    ...........
    ...........
    Bagel :
    <hutputText value="#{indexBean.selectedBagelName.items}"/>
    <hutputText value="#{indexBean.selectedBagelName.val}">
    <f:convertNumber type="currency"/>
    </hutputText>

    Topping :
    <hutputText value="#{indexBean.toppingNames}"/>
    <h:panelGroup/>

    Coffee :
    <hutputText value="#{indexBean.selectedCoffeeName.category}"/>
    <hutputText value="#{indexBean.selectedCoffeeName.prices}">
    <f:convertNumber type="currency"/>
    </hutputText>

    <h:panelGroup/>
    .........
    .........

    And... this my result after click compute Button:
    Receipt
    Bagel : White $1.25
    Topping : Butter 0.25; Peach jelly 0.75;
    Coffee : Decaf Coffee $1.25
    Subtotal : $3.50
    Tax: $0.21
    Total: $3.71

    But how to bring items and value of Topping down the line... like this:
    Bagel : White $1.25
    Topping : Butter 0.25 (And how to attached $ symbol in 0.25 of an array?)
    Peach jelly 0.75
    Coffee : Decaf Coffee $1.25
    Subtotal : $3.50
    Tax: $0.21
    Total: $3.71


    Thank you upfish... And thank you all of you.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How can I export elements in array to .xhtml

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/80880-how-can-i-export-elements-array-xhtml-jsf.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    jps (August 20th, 2013)

Similar Threads

  1. [SOLVED] matching elements of one array with another array.
    By shenaz in forum Java Theory & Questions
    Replies: 13
    Last Post: March 29th, 2013, 10:31 AM
  2. If Statements Using Elements in an Array
    By grayaa93 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2013, 08:35 AM
  3. [SOLVED] How To Increment Array Elements
    By Nuggets in forum Java Theory & Questions
    Replies: 15
    Last Post: April 1st, 2012, 12:10 PM
  4. Help camparing array elements
    By Richmond in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 12:23 AM