Úvodní stránka > Java, JSF 2, Maven > Začínáme s JSF & PrimeFaces

Začínáme s JSF & PrimeFaces

2012-11-12
  • JavaServer Faces (JSF) is a Java-based web application framework intended to simplify development integration of web-based user interfaces
  • PrimeFaces is today most used JSF component library
  1. Přidej JSF a PrimeFaces do Maven scriptu pom.xml
            <!-- JSF 2.1 -->
            <dependency>
                <groupId>javax.faces</groupId>
                <artifactId>javax.faces-api</artifactId>
                <version>2.1</version>
                <scope>provided</scope>
            </dependency>
            <!-- PrimeFaces -->
            <dependency>
                <groupId>org.primefaces</groupId>
                <artifactId>primefaces</artifactId>
                <version>3.3.1</version>
            </dependency>
    
            <!-- GlassFish repository (contains JSF) -->
            <repository>
                <id>GlassFish</id>
                <url>http://download.java.net/maven/glassfish</url>
            </repository>
            <!-- PrimeFaces repository -->
            <repository>
                <id>prime-repo</id>
                <name>Prime Repo</name>
                <url>http://repository.primefaces.org</url>
            </repository>
    
    
  2. Vytvoř POJO třídu Employee.java s getry a setry
    public class Employee {
        private int id;
        private String firstName;
        private String lastName;
    
  3. Vytvoř controller s privátní proměnnou a metodou, která ji naplní daty
    @ManagedBean
    @ViewScoped
    public class EmployeeController {
        private List<Employee> employeeList = new ArrayList<>();
    
        public void showAllEmployees() {
            Employee employee1 = new Employee();
            employee1.setId(1);
            employee1.setFirstName("John");
            employee1.setLastName("Black");
    
            Employee employee2 = new Employee();
            employee2.setId(1);
            employee2.setFirstName("Michael");
            employee2.setLastName("Knight");
    
            employeeList.add(employee1);
            employeeList.add(employee2);
        }
    
  4. Vytvoř stránku index.xhtml
    <?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" xml:lang="en" lang="en"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:p="http://primefaces.org/ui">
    
    <h:head>
        <title>Create a new employee</title>
    </h:head>
    
    <h:body>
    
        <h:form>
            <p:dataTable var="employee" value="#{employeeController.employeeList}">
                <p:column headerText="ID">
                    <h:outputText value="#{employee.id}"/>
                </p:column>
    
                <p:column headerText="First name">
                    <h:outputText value="#{employee.firstName}"/>
                </p:column>
    
                <p:column headerText="Last name">
                    <h:outputText value="#{employee.lastName}"/>
                </p:column>
            </p:dataTable>
        </h:form>
    
        <h:form>
            <h:commandButton value="Show All Employees" type="submit" action="#{employeeController.showAllEmployees}"/>
        </h:form>
    
    </h:body>
    </html>
    
    • komponenta p:dataTable je použita z PrimeFaces
    • stisknutím tlačítka dojde k vyvolání akce showAllEmployees, respektive metody showAllEmployees()
  5. Zadej URL http://127.0.0.1:9090/hr-0.1-SNAPSHOT/index.faces

Reklamy
Rubriky:Java, JSF 2, Maven