在日食开普勒中的主要观察者

时间:2014-03-13 14:32:29

标签: eclipse maven jsf primefaces

这是我的第一个使用primefaces的项目,我在动态Web项目上使用eclipse Kepler,我使用maven来处理所有依赖项。 我的项目是一个maven项目,具有春天的性质(出于其他原因)。 我也在使用最新版本的Jboss。

我创建了以下bean:

@ManagedBean
@ApplicationScoped
public class UserInterfaceBean implements Serializable{

    private static final long serialVersionUID = 2143658709;

    @Autowired
    private transient DBDataManipulatorService dbDataManipulatorService;

     private List<Aircraft> aircrafts; 

UserInterfaceBean(){

    aircrafts = dbDataManipulatorService.findAllAircrafts();
}

public List<Aircraft> getAircrafts(){

    return aircrafts;
}

}

我的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>GLOBAL REPORT</title>
</h:head>
<body>
<p:dataTable var="aircraft" value="#{UserInterfaceBean.aircrafts}">
    <f:facet name="header"> List of Aircrafts</f:facet> 
    <p:columns value="#{userinterfaceBean.aircrafts}" var="column"
        columnIndexVar="colIndex">
        <f:facet name="header"> #{column}</f:facet>
        <h:outputText value="#{userinterfaceBean.values[colIndex]}" />
    </p:columns>
</p:dataTable>
</body>
</html>

问题是我无法在查看器中获得正确的视图,我是否遗漏了某些内容,我需要做什么,或者我的代码中是否有任何错误?请给我一个详细的回复。

1 个答案:

答案 0 :(得分:0)

在构建项目并将项目部署到服务器时,您的页面是否在浏览器中正确显示?

由于你说“无法在查看器中获得正确的视图”,我猜你想在构建和部署之前预览你的xhtml页面。如果是这种情况,那么核心jsf标签可以通过eclipse预览,但我认为你不能用Primefaces标签做到这一点。

此外,为了获取bean生成的值,例如:#{UserInterfaceBean.aircrafts},您的代码必须构建并部署到服务器。

没有与Netbeans合作,但您可能想查看NetBeans 8 Loves PrimeFaces

修改

要预览xhtml页面,您需要将Dynamic Web Module方面和JavaServer Faces facet添加到项目中。

然后确保使用Web Page Editor打开您的xhtml。因此,右键单击您的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版构建ID:20140224-0627 32位,带 JBOSS工具(开普勒)4.1.1最终

将浏览器指向http://localhost:8080/PrimeSimple/并点击刷新会因为随机功能而为您带来新效果。

Class Car

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

}

* Class 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);  
    }  
}

Class 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;
    }
}

Class 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文件时出错:行数始终与列数相同,因此并非所有结果都会显示。我将评论中的旧解决方案留给其他人看。