プロジェクトの作成 †
実行してみる †
JSFプロジェクトにする †
- JSF2 のコンポーネントとしては、次の 3 つがある
実行してみる †
JSF2アプリケーションの構造 †
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>com.example</groupId>
<artifactId>JSFExam</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>JSFExam</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<url>http://repository.primefaces.org/</url>
<id>PrimeFaces-maven-lib</id>
<layout>default</layout>
<name>Repository for library PrimeFaces-maven-lib</name>
</repository>
</repositories>
</project>
faces-config.xml †
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.0"
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-facesconfig_2_0.xsd">
<application>
<locale-config>
<default-locale>ja_JP</default-locale>
</locale-config>
<resource-bundle>
<base-name>com.example.jsfexam.messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
- JSF1では、faces-config.xml に画面遷移を定義していたが、JSF2からは画面遷移は Annotations で定義する → JSF2 画面遷移?
- 残る機能はメッセージ定義のみ → JSF2 メッセージ定義
web.xml †
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.SEPARATOR_CHAR</param-name>
<param-value>_</param-value>
</context-param>
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
web.xml に定義できる JSF パラメータ †
- 必要な設定項目は 2 つ
- javax.faces.PROJECT_STAGE
- 諸々のパラメータ調整をしてくれる (キャッシュの大きさや、リソースの再読込間隔など)
- Development, UnitTest?, SystemTest?, Production
- javax.faces.SEPARATOR_CHAR
- xhtml の id の命名規約。省略時は form1:cmbbox となる。これは CSS セレクタと同じ書式などでマズー
![[sad]](image/face/sad.png)
- jQuery といっしょに使いたいなら "_" などにする
- JSF2 jQueryと組み合わせる
- その他の設定項目
javax.faces.CONFIG_FILES | 設定ファイル |
javax.faces.DEFAULT_SUFFIX | 拡張子(.xhtml) |
javax.faces.FACELETS_BUFFER_SIZE | HTTP-RESPONSE の出力バッファ。-1 : バッファなし |
javax.faces.FACELETS_REFRESH_PERIOD | xhtml の再読込間隔(秒)。-1 : 再読込しない |
javax.faces.FACELETS_SKIP_COMMENTS | <!-- --> をブラウザに返さない |
javax.faces.LIFECYCLE_ID | 独自のライフサイクルプロセッサを使う場合に、クラス名を指定する |
javax.faces.STATE_SAVING_METHOD | 状態を保存する領域。server (HTTP Session) / client (FORM 内の hidden 項目) |
javax.faces.DISABLE_FACELET_JSF_VIEWHANDLER | Faceletを無効化する |
javax.faces.LIBRARIES | JSFのライブラリ、: 区切り |
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:f="http://java.sun.com/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:outputText value="#{msg.hello}" />
<br />
<h:outputFormat value="#{msg['message.param']}" escape="false">
<f:param value="JSF2.1" />
<f:param value="PrimeFace" />
</h:outputFormat>
<br />
<h:link outcome="welcomePrimefaces" value="Primefacesようこそページ" />
</h:body>
</html>
- JSF2 から xhtml でプレゼンテーション層を定義する
- 標準で JSF と JSTL のタグが使える
- そのほかに Primefaces のタグを使う
- これに (jQuery + jQuery UI) + JAX-RS を組み合わせれば、だいたいのものは作れそう
Glassfish JSF2