John Alexander Betts
John Alexander Betts

Reputation: 5206

Data from ManagedBean doesn't refresh in the view

I am trying to update a data table with a list of products but when I call the getCatalogProducts action it doesn't refresh the view. Data doesn't appear in the view.

This is My JSF's 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;
    }
}

And this is my view:

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

When I press the gettingProducts CommandButton it calls successfully the getCatalogProducts action but it doesn't refresh the view with the data.

What is wrong?

I am putting additional information. This is my web.xml file:

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

And my pom.xml file:

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

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

Upvotes: 0

Views: 3857

Answers (2)

BalusC
BalusC

Reputation: 1108742

Apart from that <p:commandButton update> should specify components you'd like to update, you're navigating with a non-null/void outcome. In other words, you're creating a new view. In other words, the old view and all view scoped beans associated with it are destroyed. The new view will create a new view scoped bean. As view scoped beans are identified by the view, the new view does not reference the same view scoped bean anymore wherein you loaded the new list.

Just make the action method void (I'd for clarity also rename it as this is absolutely not a getter).

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

This way the same view will be reused and the existing view scoped beans associated with it will remain alive.

Second problem is that you're not binding the <p:dataTable value> to any bean property. You need to bind it to #{catalog.products} instead of to #{products} which does not exist as a managed bean.

See also:

Upvotes: 4

Ashwin Shetty
Ashwin Shetty

Reputation: 41

use update="@form".if it works then by using developer tool find the id of datable and replace @form with that id.

Upvotes: 0

Related Questions