我只想在JSF中检索结果列表。
我的JSF文件:
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<h:outputStylesheet library="css" name="css/style.css" />
<f:facet name="">
<meta http-equiv="content-type" content="text/html"/>
</f:facet>
</h:head>
<body>
<h:form>
<h:inputText value="#{CarBean.carName}" />
<h:commandButton type="submit" action="#{CarBean.result()}"
value="Set Name" />
</h:form>
<a id="InformationInputText" href="information.xhtml">Get Info</a>
<h1 id="InformationInputText">Car list:</h1>
<h:dataTable value="#{CarBean.list}" var="car">
<h:column>${car}</h:column>
</h:dataTable>
</body>
</html>
我的bean文件:
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
@ManagedBean
@RequestScoped
public class CarBean {
public CarBean() {
}
// Variables
public String carName = "";
public List<?> list;
public static final String PERSISTENCE_UNIT_NAME = "CarApp";
// Methods
public void setCarName(String carName) {
this.carName = carName;
}
public String getCarName() {
return carName;
}
public String setName() {
return carName;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
// Get result from database by select
public List<?>result() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
Query query = em
.createNativeQuery("SELECT carName,company,fuelInLitre FROM car");
list = query.getResultList();
return list;
}
}
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 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>CarApp</display-name>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<mime-mapping>
<extension>html htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
<mime-mapping>
<extension>txt</extension>
<mime-type>text/plain</mime-type>
</mime-mapping>
<mime-mapping>
<extension>css</extension>
<mime-type>text/css</mime-type>
</mime-mapping>
<mime-mapping>
<extension></extension>
<mime-type>application/octet-stream</mime-type>
</mime-mapping>
</web-app>
我收到警告
WARNING: JSF1091: No mime type could be found for file /[[Ljava.lang.Object;@6084721c, [Ljava.lang.Object;@3efe6c9e, [Ljava.lang.Object;@5c18c2c, [Ljava.lang.Object;@24f8ca75, [Ljava.lang.Object;@672f5864]. To resolve this, add a mime-type mapping to the applications web.xml.
我的JSF文件的输出:
Car list:
[Ljava.lang.Object;@3e53d993
[Ljava.lang.Object;@50c3ef5d
[Ljava.lang.Object;@34f6502e
[Ljava.lang.Object;@1c9c1393
[Ljava.lang.Object;@3c23ced2
那么问题出在哪里呢?我应该在web.xml中添加什么?我想告诉你一件事。当我写这样的东西时:
<h:dataTable value="#{CarBean.list.get(0)}" var="car">
<h:column>${car}</h:column>
</h:dataTable>
获得元素0的结果没有问题。
问题是List<?>
填充了Object[]
s,并且通过使用#{car}
JSF对数组使用toString
方法。对您来说,最好的选择是返回一个List<YourClass>
,其中YourClass
实现toString
方法而不是List<?>
。