viewscope bean在每个请求上实例化

时间:2014-04-13 14:04:13

标签: java jsf view-scope

还有其他问题,但没有一个能解决我的问题。

每当我在浏览器中刷新同一页面时,都会实例化viewscope bean(它的构造函数被调用) 这是xhtml:

<!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"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
<f:view contentType="text/html">
    <h:head>
    </h:head>
    <h:body>
        <h:form>
            <h:dataTable value="#{products.list}" var="p" id="betDt1"
                styleClass="tableSorter">

                <h:column>
                    <f:facet name="header">name </f:facet>
                    #{p.pname}
                </h:column>

            </h:dataTable>
        </h:form>
    </h:body>
</f:view>
</html>

viewscoped bean:

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

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.hibernate.Session;

import model.Product;

@ViewScoped
@ManagedBean
public class Products implements Serializable {

    private List<Product> list;
    private Session ss;

    @SuppressWarnings("unchecked")
    public Products() {
        this.ss = FaceUtils.openHibernateSession();
        System.out.println("products constructor called");
        ss.getTransaction().begin();
        list = ss.createCriteria(Product.class).list();
        this.ss.getTransaction().commit();

    }

    public List<Product> getList() {
        return list;
    }

    public void setList(List<Product> list) {
        this.list = list;
    }

    public String delete(Product pro) {
        FaceUtils.log.finest("delete pro.id" + pro.getId());

        FaceUtils.hibernateDelete(this.ss, pro);
        list.remove(pro);
        return null;
    }

    /**
     * 
     */
    private static final long serialVersionUID = -2018425860394584424L;

}

和web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>fazlastoks</display-name>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>
            index.xhtml
        </welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>org.omnifaces.FACES_VIEWS_SCAN_PATHS</param-name>
        <param-value>/*.xhtml</param-value>
    </context-param>

知道可能导致问题的原因吗?

0 个答案:

没有答案