PrimeFacesを試してみる - 環境構築編

PrimeFacesを試してみる - 環境構築編

githubに公開しました

環境:

1. プロジェクト作成

Gradleプロジェクトを作る。 f:id:clash_m45:20150510134912p:plain

gradle war pluginではweb系のファイルはsrc/main/webappに置くみたいなので、手動でソース・フォルダを作成します。
すると以下のようなフォルダ構成になります。

helloJavaEE
└─src
   ├─main
   │  ├─java
   │  ├─resources
   │  └─webapp
   │      └─WEB-INF
   └─test
       ├─java
       └─resources

2. build.gradle

gradleでJava EE構成したことなかったので、調べてみたらmaven repositoryはあるみたいっすね。

いらん記述を消して、以下build.gradleにしました。

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile(
        'commons-collections:commons-collections:3.2',
        'javax:javaee-api:7.0'
    )

    testCompile 'junit:junit:4.+'
}

3. index.xhtmlを作る

ここまででプロジェクトは出来たはずなので、index.xhtmlを書いてブラウザで表示してみます。

[新規] - [Web] - [HTMLファイル]でJSFテンプレート有りのxhtmlが作成できます。 f:id:clash_m45:20150510134918p:plain

<!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<h:head>
    <title>Hello Java EE</title>
</h:head>
<h:body>

<h1>Hello World!!</h1>

<h:outputLabel value="JSF world" />

</h:body>
</html>

4. web.xmlを作る

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <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>*.faces</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.faces</welcome-file>
    </welcome-file-list>
</web-app>

ファイル構成はこんな感じになりました。

helloJavaEE
└─src
    ├─main
    │  ├─java
    │  ├─resources
    │  └─webapp
    │      │  index.xhtml
    │      │
    │      └─WEB-INF
    │              web.xml
    │
    └─test
        ├─java
        └─resources

5. Glassfishへデプロイ!

動いた~

f:id:clash_m45:20150510135005p:plain

補足. gradleでglassfishにデプロイする

毎回gradle warしてglassfishにデプロイするの面倒だな~と思ったら出来ました。

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile(
        'commons-collections:commons-collections:3.2',
        'javax:javaee-api:7.0'
    )

    testCompile 'junit:junit:4.+'
}

ext {
    glassFishHome = 'C:/glassfish-4.1/glassfish4'
    asadmin = glassFishHome + (isWindows() ? '/bin/asadmin.bat'
                                           : '/bin/asadmin')
    domain = 'domain1'
}

def isWindows() {
    return System.properties['os.name'].toLowerCase().contains('windows')
}

task startServer(type: Exec) {
    commandLine asadmin, 'start-domain', '--debug=true', domain
}

task stopServer(type: Exec) {
    commandLine asadmin, 'stop-domain', domain
}

task deploy(type: Exec, dependsOn: 'war') {
    commandLine asadmin, 'deploy', '--force=true', war.archivePath
}

ほんで、eclipsegradle タスクビューを開いて、ポチポチっとするとデプロイ出来るようになりました。

f:id:clash_m45:20150510135019p:plain

デバッグeclipseの[デバッグの構成] - [リモートJavaアプリケーション]で実行できるようです。(まだ試してない)

6. Primefacesを試す

タイトルがPrimeFacesだったのを思い出しました。

build.gradleにPrimeFacesの依存関係を追加します。

dependencies {
    compile(
        'commons-collections:commons-collections:3.2',
        'javax:javaee-api:7.0',
        'org.primefaces:primefaces:5.2'
    )

    testCompile 'junit:junit:4.+'
}

試す!

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

<h:head>
    <title>Hello Java EE</title>
</h:head>
<h:body>

<h1>Hello World!!</h1>

<h:outputLabel value="JSF world" />

<p:spinner />

</h:body>
</html>

どや~ f:id:clash_m45:20150510135034p:plain