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?