Contoh Prime Faces

Prime faces adalah web framework yang support bahasa pemrograman java, karena basis prime faces adalah JSF ( JavaServer Faces ), maka tidak akan sulit kalau anda mempelajarinya lebih lanjut.

Java 1.8
Maven 3.7.0
Tomcat 7
Prime Faces 6.1

Stuktur Program


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>co.id</groupId>
    <artifactId>Aplikasi-Prime-Faces</artifactId>
    <version>1.0-SNAPHOT</version>
    <packaging>war</packaging>
    
    <properties>
        <java.version>1.8</java.version>
        <servlet.version>3.1.0</servlet.version>
        <jsf.version>2.2.15</jsf.version>
        <primefaces.version>6.1</primefaces.version>

        <!--appassembler-maven-plugin.version>2.0.0</appassembler-maven-plugin.version>
        <jetty-maven-plugin.version>9.4.8.v20171121</jetty-maven-plugin.version-->

        <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
        <tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
    </properties>
    
    <dependencies>
        <!-- Servlet -->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>${servlet.version}</version>
          <scope>provided</scope>
        </dependency>
        <!-- JSF -->
        <dependency>
          <groupId>com.sun.faces</groupId>
          <artifactId>jsf-api</artifactId>
          <version>${jsf.version}</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>com.sun.faces</groupId>
          <artifactId>jsf-impl</artifactId>
          <version>${jsf.version}</version>
          <scope>compile</scope>
        </dependency>
        <!-- PrimeFaces -->
        <dependency>
          <groupId>org.primefaces</groupId>
          <artifactId>primefaces</artifactId>
          <version>${primefaces.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- Compile java -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>           
            <!-- Run with Jetty  -->
            <!--plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
                <version>${appassembler-maven-plugin.version}</version>
            </plugin>    
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty-maven-plugin.version}</version>
                <configuration>
                    <webAppConfig>
                        <contextPath>/${project.artifactId}</contextPath>
                        <allowDuplicateFragmentNames>true</allowDuplicateFragmentNames>
                    </webAppConfig>
                    <scanIntervalSeconds>0</scanIntervalSeconds>
                </configuration>
            </plugin-->
            <!-- Run with Tomcat  -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>${tomcat7-maven-plugin.version}</version>
                <configuration>
                    <path>/${project.artifactId}</path>
                </configuration>
            </plugin>     
        </plugins>
    </build>
</project>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <!-- Define the JSF servlet (manages the request processing life cycle for 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>

    <!-- Map following files to the JSF servlet -->
    <servlet-mapping>
        <servlet-name>faces-servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
  
    <!-- File(s) appended to a request for a URL that is not mapped to a web component -->
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>


Index.java
package co.id.teko.sulaiman;

import javax.faces.bean.ManagedBean;

/**
 *
 * @author teko.sulaiman@gmail.com
 */
@ManagedBean
public class Index {
    private String string;
    
    public Index(){
        string = "Hello Prime Faces";
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }
}

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"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui">

    <h:head>
      <title>Contoh Prime Faces</title>
    </h:head>

    <h:body>
        <h:outputText id="display" value="#{index.string}" />
    </h:body>
</html>

Output

No comments:

Post a Comment