0

I want to add some objects to a list using JSF. My problem is that I do not know exactly how to create instances of objects without declaring an empty instance and after that to use setters.

So, through the following code I just want to update a List of Laptops. Apparently it works, but I am not sure if this is the right way to do the things.

Here are my classes:

Laptop.java:

public class Laptop
{
    public String name;
    private double price;
    private int    quantity;

    public Laptop( String name,
                   double price,
                   int quantity )
    {
        super( );
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }

    public String getProductName( )
    {
        return name;
    }

    public void setName( String name )

    {
        this.name = name;
    }

    public double getPrice( )
    {
        return price;
    }

    public void setPrice( double price )
    {
        this.price = price;
    }

    public int getQuantity( )
    {
        return quantity;
    }

    public void setQuantity( int quantity )
    {
        this.quantity = quantity;
    }
}

LaptopBean.java:

@ManagedBean( name = "laptopBean" )
@RequestScoped
public class LaptopBean
{
    private String        _laptopName;
    private double        _price;
    private  int           _quantity;

    private List<Laptop> laptops = new ArrayList( );

    public LaptopBean( )
    {
        laptops = new ArrayList( );
    }

    public List<Laptop> getLaptops( )
    {
        return laptops;
    }

    public String getName( )
    {
        return _laptopName;
    }

    public void setName( String _laptopName )
    {
        this._laptopName = _laptopName;
    }

    public double getPrice( )
    {
        return _price;
    }

    public void setPrice( double _price )
    {
        this._price = _price;
    }

    public int getQuantity( )
    {
        return _quantity;
    }

    public void setQuantity( int _quantity )
    {
        this._quantity = _quantity;
    }

    public void addToList() {
        laptops.add(new Laptop(_laptopName, _price, _quantity));

    }
}

addNewLaptop.xhtml

<!DOCTYPE html>
<h:html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd"
    xmlns:h="http://java.sun.com/jsf/html" xml:lang="en">

<h:head>
<title>Add Laptop</title>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:body>

<h:form>
<h:outputLabel>Laptop:</h:outputLabel>
<h:inputText id="name" value="#{laptopBean.name}"></h:inputText>
<br/>

<h:outputLabel>Quantity:</h:outputLabel>
<h:inputText id="quantity" value="#{laptopBean.quantity}"></h:inputText>
<br/>

<h:outputLabel>Price:</h:outputLabel>
<h:inputText id="price" value="#{laptopBean.price}"></h:inputText>
<br/>

<h:commandButton value="Add to list!" action="#{laptopBean.addToList()}"></h:commandButton>
</h:form>


    <h:dataTable value="#{laptopBean.laptops}" var="o">
        <h:column>
            <!-- Column header -->

            <!--  row record -->
            #{o.productName}
            </h:column>

        <h:column>
            <!-- Column header -->

            <!--  row record -->
            #{o.quantity}
            </h:column>


        <h:column>
            <!-- Column header -->

            <!--  row record -->
            #{o.price}
            </h:column>
    </h:dataTable>



</h:body>
</h:html>

What I've tried is to make from LaptopBean a kind of Builder and to make an instance of Laptop from it's variables, but I am not sure if my code looks fine. In my opinion it is a very messy.

Is there any other clearer way to create an instance of object by using the the inputText of the user?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
John R.
  • 420
  • 4
  • 14
  • Why don't you just create an instance of `Laptop` in `LaptopBean`, use that instance in your page and when you save you pass that created instance where needed? Btw, your xhtml looks messy because of the formatting. – Thomas Jan 17 '19 at 13:36
  • As your LaptopBean is `@RequestScoped`, does your table ever show more than one Laptop? I think it will be reset on every form submission because the List `LaptopBean.laptops` is recreated on every request. – Selaron Jan 17 '19 at 14:23
  • Hi, please start with https://stackoverflow.com/questions/8463178/what-to-use-managed-beans-backing-beans-or-entity-beans and https://stackoverflow.com/questions/10301363/jpa-entity-as-jsf-bean and the links in the latter. – Kukeltje Jan 17 '19 at 19:31
  • @Selaron you are right. It resets on every new submission :|. – John R. Jan 18 '19 at 05:34

1 Answers1

0

In my experience, I use like as below.

Just create empty Laptop object in @Postconstract method for initialize state.

NOTE : create default constructor in Laptop class. Your bean

@ManagedBean( name = "laptopBean" )
@RequestScoped
public class LaptopBean {

    private Laptop laptop;
    private List<Laptop> laptops;
    //getter and setter

    @PostConstruct
    public void init() {
        this.Laptop = new Laptop(); /// NOTE : create default constructor in Laptop class.
        this.laptops = new ArrayList( );
    }

    public void addToList() {
        laptops.add(laptop);
    }
}

in your page, you can point the created laptop object like as below

....

<h:inputText id="name" value="#{laptopBean.laptop.name}"></h:inputText>
<br/>

<h:outputLabel>Quantity:</h:outputLabel>
<h:inputText id="quantity" value="#{laptopBean.laptop.quantity}"></h:inputText>
<br/>

<h:outputLabel>Price:</h:outputLabel>
<h:inputText id="price" value="#{laptopBean.laptop.price}"></h:inputText>

...
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131