如何在JSF中创建对象的实例

时间:2019-01-17 13:28:37

标签: jsf

我想使用JSF将一些对象添加到列表中。我的问题是我不知道如何确切地创建对象实例而不声明一个空实例,然后再使用setter。

因此,通过以下代码,我只想更新笔记本电脑列表。显然可以,但是我不确定这是否是正确的方法。

这是我的课程:

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>

我尝试过的工作是从LaptopBean中构建一种Builder,并从其变量中创建一个Laptop实例,但是我不确定我的代码是否正常。我认为这很混乱。

还有其他更清晰的方法可以通过使用用户的inputText创建对象的实例吗?

1 个答案:

答案 0 :(得分:0)

根据我的经验,我的用法如下。

只需使用Laptop方法创建空的@Postconstract对象即可初始化状态。

注意:在Laptop类中创建默认构造函数。 你的豆子

@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);
    }
}

在页面中,您可以如下所示指向创建的笔记本电脑对象

....

<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>

...