0

我正在尝试使用产品列表更新数据表,但是当我调用 getCatalogProducts 操作时,它不会刷新视图。数据不会出现在视图中。

这是我的 JSF 的 ManagedBean:

@ManagedBean(name = "catalog")
@ViewScoped
public class CatalogMgtBean {

    private String value = "This is the value";
    private List<Port> products;
    private CatalogService catalogService;

    public void loadCatalogProducts() {

        catalogService = new CatalogServiceImpl();

        products = catalogService.getProducts();

        this.value = "This is another value";
    }

    public List<Port> getProducts() {
        return products;
    }

    public void setProducts(List<Port> products) {
        this.products = products;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

这是我的观点:

<?xml version="1.0" encoding="UTF-8"?>
<!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:p="http://primefaces.org/ui">

<h:head>
</h:head>
<h:body>
    <h:form id="catalogForm">
        <p:dataTable id="productsDT" paginator="true" paginatorPosition="bottom"
            rows="10" value="#{products}" var="products">
            <p:column headerText="ID">
                <h:outputText value="#{products.id}" />
            </p:column>
            <p:column headerText="ID">
                <h:outputText value="#{products.nombre}" />
            </p:column>
            <p:column headerText="ID">
                <h:outputText value="#{products.nacional}" />
            </p:column>
            <p:column headerText="FECHA">
                <h:outputText value="#{products.fecha}">
                    <f:convertDateTime pattern="dd/MM/yyyy" />
                </h:outputText>
            </p:column>
            <p:column headerText="ESTADO">
                <h:outputText value="#{products.estado}" />
            </p:column>
        </p:dataTable>
        <p:commandButton id="gettingProducts" value="Getting the products..."
        action="#{catalog.loadCatalogProducts()}" update="productsDT" />
    </h:form>
</h:body>
</html>

当我按下 gettingProducts 命令按钮时,它会成功调用 getCatalogProducts 操作,但不会用数据刷新视图。

怎么了?

我正在提供其他信息。这是我的 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" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

  <display-name>JSAC</display-name>

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<!-- Welcome page -->
<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>

<!-- Servlets -->
<!-- JavaServer Faces -->
<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>

还有我的 pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>JSAC</groupId>
    <artifactId>JSAC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>JSAC</name>

    <properties>

        <!-- Generic properties -->
        <java.version>1.6</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Web -->
        <jsp.version>2.2</jsp.version>
        <jstl.version>1.2</jstl.version>
        <servlet.version>2.5</servlet.version>

    </properties>

    <repositories>
        <repository>
            <id>prime-repo</id>
            <name>Prime Repo</name>
            <url>http://repository.primefaces.org</url>
        </repository>
    </repositories>

    <dependencies>

        <!-- Primefaces -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>3.3</version>
        </dependency>

        <!-- JSF -->
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.11</version>
        </dependency>

        <!-- Postgres DB -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.0-801.jdbc4</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp.version}</version>
        </dependency>

        <!-- Javamelody -->
        <dependency>
            <groupId>net.bull.javamelody</groupId>
            <artifactId>javamelody-core</artifactId>
            <version>1.46.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

还有 faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1_24.xsd" version="2.1">

    <application>
        <resource-bundle>
            <base-name>i18n.messages</base-name>
            <var>msg</var>
        </resource-bundle>
    </application>
</faces-config>
4

2 回答 2

4

除此之外<p:commandButton update>应该指定您想要更新的组件,您正在使用非null/void结果导航。换句话说,您正在创建一个视图。换句话说,旧视图和与之关联的所有视图范围的 bean 都被销毁了。新视图将创建一个新的视图范围 bean。由于视图范围 bean 由视图标识,因此新视图不再引用您在其中加载新列表的同一个视图范围 bean。

只需制作操作方法void(为了清楚起见,我还要重命名它,因为这绝对不是吸气剂)。

public void loadCatalogProducts() {
    catalogService = new CatalogServiceImpl();
    products = catalogService.getProducts();
    this.value = "This is another value";
}

这样,相同的视图将被重用,并且与之关联的现有视图范围 bean 将保持活动状态。

第二个问题是您没有将 绑定<p:dataTable value>到任何 bean 属性。您需要将其绑定到#{catalog.products}#{products}作为托管 bean 存在的对象上。

也可以看看:

于 2013-08-16T13:39:44.583 回答
0

使用 update="@form"。如果它有效,则使用开发人员工具找到 datable 的 id 并用该 id 替换 @form。

于 2013-08-16T13:19:57.500 回答