2

当我打开 jsf 页面然后catalog bean创建两次。那么为什么会发生呢?jsf页面:

<h:dataTable value="#{catalog.products}" var="p">
    <h:column>
        <h:form>
            <h:commandLink action="detail?faces-redirect=true"
                value="#{p.brand} #{p.model}">
                <f:setPropertyActionListener value="#{p}"
                    target="#{ph.currentProduct}" />
            </h:commandLink>
        </h:form>
    </h:column>
</h:dataTable>

豆:

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.inject.Inject;
import javax.inject.Named;

import dataLayer.Dao;

@Named
@RequestScoped
public class Catalog implements Serializable {

    @Inject
    private Dao dao;

    private List<Product> products;
    HtmlDataTable dataTable;

    public Catalog() {
    }

    @PostConstruct
    private void init() {
        System.out.println("catalog bean created");
        dataTable = new HtmlDataTable();
        products = dao.getShavers();
    }
...
4

0 回答 0