0

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>
John Alexander Betts
  • 4,718
  • 8
  • 47
  • 72

2 Answers2

4

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I did the changes @BalusC and the dataTable doesn't refresh – John Alexander Betts Aug 16 '13 at 13:47
  • I am using JSF without Spring right now as it as you suggested me. I will put another files to provide more information. @BalusC I did the changes that erencan suggests to me but now I have my files as is as shown in this post – John Alexander Betts Aug 16 '13 at 13:55
  • There are no error, warnings or exceptions printed in server logs and not in chrome console @BalusC. My method in the ManagedBean is called but in the browser seem to be that no changes are made. I don't know how can I do to monitor ajax calls – John Alexander Betts Aug 16 '13 at 14:06
  • Yes @BalusC I put an id to the form name catalogForm and it appears in this way: – John Alexander Betts Aug 16 '13 at 14:18
  • Yes @BalusC. The table appears with the right header with the "No records found" message – John Alexander Betts Aug 16 '13 at 14:21
  • I debugged the ManagedBean before I asked this question and then each time I test the new changes you have suggested to me @BalusC. My method always get the products in the right way – John Alexander Betts Aug 16 '13 at 14:28
  • My method is returning a non-empty list. When I press the command button at the second time, in the Backing Bean appears the product attribute with the list. But the view seems to be not able to show the information @BalusC. The list is good all the time, the ViewScoped annotation is working well but the view doesn't – John Alexander Betts Aug 16 '13 at 14:35
  • Just in case you need @BalusC. When I ask this question at first time I didn't have the faces-config.xml file and my view could call my Backing bean perfectly but never show the products in the view. I added the facees-config.xml file and nothing happend. The two things that you told me are right, the problem is in the view – John Alexander Betts Aug 16 '13 at 15:00
  • The table in the view always is empty. @BalusC when I put the command button at first time before the method in the backing bean call the method in the service the products attribute of the backing bean is null, then when the service method which get the products is called it retunrs the list of products with 13 elements but don't appear in the view. When I press the command button the second time and later when it calls the backing bean method the products attribute is not empty. It means that product remain in the scope successfully but the view don't refresh the values in the table – John Alexander Betts Aug 16 '13 at 15:11
  • The table is always empty before the button is pressed @BalusC – John Alexander Betts Aug 16 '13 at 15:12
  • I have found the error @BalusC the correct value attribute of the dataTable must be #{catalog.products} and not #{products}. But you solved the main problem. – John Alexander Betts Aug 16 '13 at 15:27
0

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

Ashwin Shetty
  • 41
  • 1
  • 1
  • 6