我正在尝试使用产品列表更新数据表,但是当我调用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 CommandButton时,它会成功调用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>
答案 0 :(得分: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存在。
答案 1 :(得分:0)
使用update =“@ form”。如果它工作,那么通过使用开发人员工具找到datable的id并用该id替换@form。