最近の更新

2013年7月29日月曜日

h:commandButtonのactionで固定画面遷移

【目的】
JSF2.2でh:commandButtonを実行します。
commandButtonのactionで画面を固定で遷移します。



【手順1】
1.「JSF2.2プロジェクトの作成方法」の作成手順で、「JSF22Sample101-h-commandButtonAction」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
ウィザード名 項目名 項目に設定する値
動的 Web プロジェクト プロジェクト名 JSF22Sample101-h-commandButtonAction
ターゲット・ランタイム GlassFish 4.0
動的 web モジュールバージョン 3.1
プロジェクト・ファセット JavaServer Faces チェック有り バージョン(2.2)
Java ビルド・パス上のソース・フォルダー src
デフォルト出力フォルダー build\classes
Web モジュール コンテキスト・ルート JSF22Sample101-h-commandButtonAction
コンテンツ・ディレクトリー WebContent
web.xml デプロイメント記述子の作成 チェックあり
JSF 機能 JSF 実装ライブラリー GlassFish System Library
Configure JSF servlet in deployment descriptor チェック有り
JSF 構成ファイル /WEB-INF/faces-config.xml
JSF サーブレット名 Faces Servlet
JSF Servlet Class Name javax.faces.webapp.FacesServlet
URL マッピング・パターン /faces/*



2.「WebContent/WEB-INF/web.xml」を以下の様に入力。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <display-name>JSF22Sample101-h-commandButtonAction</display-name>

    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>

    <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>
</web-app>

4.「Ctrl+Shift+F」を押し、ソースコードをフォーマッティング、「Ctrl+S」でファイルを保存。
5.「任意のファイルの作成方法」の手順で、「WebContent/」ディレクトリに「index.xhtml」というファイルを作成。
6.「index.xhtml」を以下の様に入力。
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
</h:head>
<h:body>
    <h:form>
        <h:commandButton value="移動" action="page2" />
    </h:form>
</h:body>
</html>

7.「任意のファイルの作成方法」の手順で、「WebContent/」ディレクトリに「page2.xhtml」というファイルを作成。
8.「page2.xhtml」を以下の様に入力。
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
</h:head>
<h:body>
    ページ2です。
</h:body>
</html>

9.「Ctrl+Shift+F」を押し、ソースコードをフォーマッティング、「Ctrl+S」でファイルを保存。
10.「動的Webアプリケーションをサーバーに配置する方法」の手順で、作成したプロジェクトをサーバーに配置します。
11.「サーバーをデバッグモードで起動する方法」の手順で、サーバーを起動します。
12.ブラウザで以下のURLにアクセスします。
http://localhost:8080/JSF22Sample101-h-commandButtonAction/faces/index.xhtml

13.以下の様に表示されれば成功です。



































14.ソースコードは以下の様に出力されていました。(見やすく整形しています)
<html xmlns="http://www.w3.org/1999/xhtml">
    <head id="j_idt2">
    </head>
    <body>
        <form id="j_idt4" name="j_idt4" method="post" action="/JSF22Sample101-h-commandButtonAction/faces/index.xhtml" enctype="application/x-www-form-urlencoded">
            <input type="hidden" name="j_idt4" value="j_idt4" />
            <input type="submit" name="j_idt4:j_idt5" value="移動" />
            <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="5062835420762175325:7914364872365477917" autocomplete="off" />
        </form>
    </body>
</html>



【結論】
「input type="submit"」のボタンが出力されました。
特に画面の遷移先は記載されていません。
サーバ側で処理されると思われます。



以上です。

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。

関連記事