當您將項目構建並部署到服務器時,您的頁面是否在瀏覽器中正確顯示?
既然你說「無法在查看器中獲得正確的視圖」我猜你希望在構建和部署之前預覽你的xhtml頁面。如果是這種情況,那麼Eclipse的核心jsf標籤可以預覽,但我認爲你不能用Primefaces標籤來做到這一點。
此外,爲了獲得由bean生成的值,例如:#{UserInterfaceBean.aircrafts}
那麼您的代碼必須構建並部署到服務器。
有沒有使用Netbeans的工作,但也許你想檢查NetBeans 8 Loves PrimeFaces
編輯
爲了預覽xhtml
頁面,您需要具備的Dynamic Web Module
面和JavaServer Faces facet
添加到您的項目。
然後確保您的xhtml用Web Page Editor
打開。所以右鍵單擊您的xhtml文件並使用Web Page Editor
打開。
嘗試僅使用核心JSF標記作爲您的第一個項目,因爲像Primefaces這樣的庫不能用於預覽。
這些bean由服務器管理並運行,因此爲了預覽由bean生成的值,您需要讓代碼在服務器上運行,因爲最終的xhtlm將由這些bean的值生成提供。
這些是一般規則,以便您可以使用eclipse。
EDIT2
根據您提供xhtmml和你正在嘗試做的,這個例子是什麼(已經改變了一點):datatableDynamicColumns這裏是一些代碼,可能會有幫助。對我來說,它可以在JBOSS EAP 6.2和WildFly 8.0.0上運行。我也使用適用於Web開發人員的Eclipse Java EE IDE。版本:開普勒服務版本2版本號:20140224-0627 32位與JBOSS工具(開普勒)4.1。1決賽
指向您的瀏覽器到http://localhost:8080/PrimeSimple/
並點擊刷新將帶來新的結果,因爲隨機函數。
級車
package webapp;
public class Car
{
private String model;
private int year;
private String manufacturer;
private String color;
public Car(String model, int year, String manufacturer, String color) {
this.model = model;
this.year = year;
this.manufacturer = manufacturer;
this.color = color;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
類公司
package webapp;
import java.util.List;
public class Company
{
private String name;
private List<Car> carList;
//needed only for comparison
private Integer carlistSize;
public Company() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Car> getCars() {
return carList;
}
public void setCars(List<Car> carList) {
this.carList = carList;
carlistSize=carList.size();
}
public Integer getCarListSize() {
return carlistSize;
}
}
* 類PopulateCar *
package webapp;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class PopulateCar
{
private final static String[] colors;
private final static String[] manufacturers;
static {
colors = new String[10];
colors[0] = "Black";
colors[1] = "White";
colors[2] = "Green";
colors[3] = "Red";
colors[4] = "Blue";
colors[5] = "Orange";
colors[6] = "Silver";
colors[7] = "Yellow";
colors[8] = "Brown";
colors[9] = "Maroon";
manufacturers = new String[10];
manufacturers[0] = "Mercedes";
manufacturers[1] = "BMW";
manufacturers[2] = "Volvo";
manufacturers[3] = "Audi";
manufacturers[4] = "Renault";
manufacturers[5] = "Opel";
manufacturers[6] = "Volkswagen";
manufacturers[7] = "Chrysler";
manufacturers[8] = "Ferrari";
manufacturers[9] = "Ford";
}
public static List<Car> findAllCars(int size)
{
List<Car> carsSmall = new ArrayList<Car>();
for(int i = 0 ; i < size ; i++)
carsSmall.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));
return carsSmall;
}
private static int getRandomYear() {
return (int) (Math.random() * 50 + 1960);
}
private static String getRandomColor() {
return colors[(int) (Math.random() * 10)];
}
private static String getRandomManufacturer() {
return manufacturers[(int) (Math.random() * 10)];
}
private static String getRandomModel() {
return UUID.randomUUID().toString().substring(0, 8);
}
}
類PopulateCompany
package webapp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;
public class PopulateCompany
{
private final static String[] compNames;
static {
compNames = new String[8];
compNames[0] = "Company1";
compNames[1] = "Company2";
compNames[2] = "Company3";
compNames[3] = "Company4";
compNames[4] = "Company5";
compNames[5] = "Company6";
compNames[6] = "Company7";
compNames[7] = "Company8";
}
public static List<Company> findAllCompanies(int compSize)
{
List<Company> companyList = new ArrayList<Company>();
for (int i=0;i<compSize;i++){
Company company = new Company();
company.setName(compNames[i]);
int numOfCars = 1 + (int)(Math.random() * ((10 - 1) + 1));
List<Car> carsOfCompany = PopulateCar.findAllCars(numOfCars);
company.setCars(carsOfCompany);
companyList.add(company);
}
Collections.sort(companyList, new Comparator<Company>() {
@Override
public int compare(Company c1, Company c2) {
return c2.getCarListSize().compareTo(c1.getCarListSize());
}
});
return companyList;
}
}
類TableBeanDynamic
package webapp;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
@ManagedBean
@RequestScoped
public class TableBeanDynamic implements Serializable {
private List<String> columns;
private List<Car[]> dynamicCars;
private String columnName;
private List<Company> companies;
public TableBeanDynamic() {
int numOfCompanies = 1 + (int)(Math.random() * ((8 - 1) + 1));
companies = PopulateCompany.findAllCompanies(numOfCompanies);
createDynamicColumns();
populateDynamicCars();
}
private void createDynamicColumns() {
columns = new ArrayList<String>();
for(int i=0; i < companies.size(); i++) {
columns.add(companies.get(i).getName());
}
}
private void populateDynamicCars() {
dynamicCars = new ArrayList<Car[]>();
for(int i=0; i < columns.size(); i++)
{
Car[] cars = new Car[companies.get(i).getCars().size()];
for(int j=0; j < cars.length; j++) {
cars[j] = new Car( companies.get(i).getCars().get(j).getModel(),
companies.get(i).getCars().get(j).getYear(),
companies.get(i).getCars().get(j).getManufacturer(),
companies.get(i).getCars().get(j).getColor());
}
dynamicCars.add(cars);
}
}
public List<String> getColumns() {
return columns;
}
public List<Car[]> getDynamicCars() {
return dynamicCars;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
}
的index.xhtml
<!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>
<title>PrimeFaces DynamicColumnTable Example</title>
</h:head>
<h:body>
<!-- <h:form>
<p:dataTable var="cars" value="#{tableBeanDynamic.dynamicCars}" id="carsTable">
<p:columns value="#{tableBeanDynamic.columns}" var="column" columnIndexVar="colIndex">
<f:facet name="header">
<p:outputPanel>
#{column}
</p:outputPanel>
</f:facet>
<h:outputText value="#{cars[colIndex].model}" /><br />
<h:outputText value="#{cars[colIndex].year}" /> <br />
<h:outputText value="#{cars[colIndex].color}" style="color:#{cars[colIndex].color}"/>
</p:columns>
</p:dataTable>
</h:form> -->
<h:form>
<!-- dynamicCars list is ordered. So I get the first Car[] element from the list which has the maximum number
of elements so that p:dataTable will generate that many rows as the number of elements in the first Car[] of
the list -->
<p:dataTable var="cars" value="#{tableBeanDynamic.dynamicCars.get(0)}" rowIndexVar="rowIndex" id="carsTable">
<p:columns value="#{tableBeanDynamic.columns}" var="column" columnIndexVar="colIndex">
<f:facet name="header">
<p:outputPanel>
#{column}
</p:outputPanel>
</f:facet>
<h:outputText value="#{tableBeanDynamic.dynamicCars[colIndex][rowIndex].model}" /><br />
<h:outputText value="#{tableBeanDynamic.dynamicCars[colIndex][rowIndex].year}" /> <br />
<h:outputText value="#{tableBeanDynamic.dynamicCars[colIndex][rowIndex].color}" style="color:#{tableBeanDynamic.dynamicCars[colIndex][rowIndex].color}"/>
</p:columns>
</p:dataTable>
</h:form>
</h:body>
</html>
的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"
version="3.0">
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<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>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>so.examples</groupId>
<artifactId>PrimeSimple</artifactId>
<packaging>war</packaging>
<version>0.0.1</version>
<description>Simple Primefaces Example</description>
<properties>
<!-- Explicitly declaring the source encoding eliminates the following
message: -->
<!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered
resources, i.e. build is platform dependent! -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- JBoss dependency versions -->
<version.jboss.maven.plugin>7.4.Final</version.jboss.maven.plugin>
<!-- Define the version of the JBoss BOMs we want to import to specify
tested stacks. -->
<version.jboss.bom>1.0.7.Final</version.jboss.bom>
<!-- Alternatively, comment out the above line, and un-comment the line
below to use version 1.0.4.Final-redhat-4 which is a release certified to
work with JBoss EAP 6. It requires you have access to the JBoss EAP 6
maven repository. -->
<!-- <version.jboss.bom>1.0.4.Final-redhat-4</version.jboss.bom>> -->
<!-- other plugin versions -->
<version.surefire.plugin>2.10</version.surefire.plugin>
<version.war.plugin>2.1.1</version.war.plugin>
<!-- maven-compiler-plugin -->
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
</properties>
<repositories>
<repository>
<id>prime-repo</id>
<name>Prime Repo</name>
<url>http://repository.primefaces.org</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<!-- JBoss distributes a complete set of Java EE 6 APIs including a Bill
of Materials (BOM). A BOM specifies the versions of a "stack" (or a collection)
of artifacts. We use this here so that we always get the correct versions
of artifacts. Here we use the jboss-javaee-6.0-with-tools stack (you can
read this as the JBoss stack of the Java EE 6 APIs, with some extras tools
for your project, such as Arquillian for testing) -->
<dependency>
<groupId>org.jboss.bom</groupId>
<artifactId>jboss-javaee-6.0-with-tools</artifactId>
<version>${version.jboss.bom}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- First declare the APIs we depend on and need for compilation. All
of them are provided by JBoss AS 7 -->
<!-- Import the CDI API, we use provided scope as the API is included in
JBoss AS 7 -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Import the Common Annotations API (JSR-250), we use provided scope
as the API is included in JBoss AS 7 -->
<dependency>
<groupId>org.jboss.spec.javax.annotation</groupId>
<artifactId>jboss-annotations-api_1.1_spec</artifactId>
<scope>provided</scope>
</dependency>
<!-- Import the EJB API, we use provided scope as the API is included in
JBoss AS 7 -->
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<scope>provided</scope>
</dependency>
<!-- Import the JSF API, we use provided scope as the API is included in
JBoss AS 7 -->
<dependency>
<groupId>org.jboss.spec.javax.faces</groupId>
<artifactId>jboss-jsf-api_2.1_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>all-themes</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<!-- Maven will append the version to the finalName (which is the name
given to the generated war, and hence the context root) -->
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>${version.war.plugin}</version>
<configuration>
<!-- Java EE 6 doesn't require web.xml, Maven needs to catch up! -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- The JBoss AS plugin deploys your war to a local JBoss AS container -->
<!-- To use, run: mvn package jboss-as:deploy -->
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>${version.jboss.maven.plugin}</version>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.1.Final</version>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<!-- The default profile skips all tests, though you can tune it to run
just unit tests based on a custom pattern -->
<!-- Seperate profiles are provided for running all tests, including Arquillian
tests that execute in the specified container -->
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.surefire.plugin}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
檢查其是否正常工作後,構建和部署,不能預覽,我們已經討論過將無法正常工作。如果有效,那麼您需要了解如何與數據庫連接,並創建返回的實際查詢(在示例中爲每個包含汽車列表的公司列表)。
編輯在前面的xhtml
文件的視圖中存在錯誤:行數總是與列數相同,因此不會顯示所有結果。我將留下的舊解決方案留給別人看。
如果你想看到頁面預覽,你應該在eclipse market中安裝Jboss工具。它不是kepler的問題,因爲我正在使用它來檢查'XHTML'頁面預覽。我在你的發佈代碼中看到幾個問題。你不應該在JSF Managed Bean中使用'@ Autowired'來注入彈簧豆。你應該使用'@ ManagedProperty',並且你應該將數據表放在一個表單中。並且爲了一致性而使'
'變爲'我已經安裝了Jboss工具,預覽不起作用。對於@Autowired,我不明白你的意思,autowired屬性是我調用的從數據庫檢索數據的服務,我在這個bean的構造函數中使用它。你可以使用一些代碼來獲得更多細節。 – Guizmoo03
http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/。在這個例子中,如果你看到基於註釋的例子,他使用'@ component'along和'@ managedBean'來確保'@ Autowired'工作。但在你的情況下,'UserInterfaceBean'是一個純JSF控制的託管bean,你試圖在'Autowire'彈簧託管的bean中。 – SRy