Contoh Hello World JSF

Contoh membuat Hello World di JSF 2.

Kali ini saya akan memberikan contoh bahasa pemrograman java berbasis web dengan menggunakan web framework JSF.

Step by stepnya sebagai berikut.

Langkah pertama :
Buatlah sebuah project dengan menggunakan maven, anda bisa membuat project maven dari CMD (Windows)  atau langsung dari IDE (Netbeans, Eclipse, etc). Di tuorial ini saya menggunakan CMD. Arahkan path ke directory biasa anda membuat project java di Netbeans.


Kemudian tekan enter, jika berhasil printoutnya seperti ini


Langkah berikutnya :
Buka project maven terserbut di IDE Netbeans, maka otomatis netbeans akan mengenalinya sebagai project maven.


Langkah berikutnya:
Tambahkan syntax xml di 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-JSF</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>
        <themeprimefaces.version>1.0.10</themeprimefaces.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>
    </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>

Langkah selanjutnya tambahkan syntax di 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>

Langkah selanjutnya membuat index.xhtml >  Web Pages

<!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://xmlns.jcp.org/jsf/core">
    <head>
        <title>Hello JSF 2!</title>
    </head>
    <body>
       #{index.stringLabel}
    </body>
</html>

Langkah selanjutnya membuat Index.java > src

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class Index {
    
    private String stringLabel;

    public Index() {
        System.out.println("WelcomeBean instantiated");
        stringLabel ="Hello JSF 2";
    }

    public String getStringLabel() {
        return stringLabel;
    }

    public void setStringLabel(String stringLabel) {
        this.stringLabel = stringLabel;
    }
}

Langkah terakhir ketik perintah mvn clean install jetty:run

Outputnya

No comments:

Post a Comment