最近の更新

2012年6月14日木曜日

(PrimeFaces ShowCase)Download

【目的】
PrimeFaces ShowCaseのFileDownloadを実行します。



【手順】
1.「PrimeFacesプロジェクトの作成方法」の手順で、「PrimeFacesSample062-Download」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。

動的
Web
プロジェクト
プロジェクト名PrimeFacesSample062-Download

ターゲット・ランタイムApache Tomcat v6.0
動的 web モジュール バージョン2.5
構成Apache Tomcat v6.0 デフォルト構成
EARメンバーシップチェックなし
ワーキング・セットチェックなし
Javaビルド・パス上のソース・フォルダーsrc
デフォルト出力フォルダーbuild\classes
Web
モジュール
コンテキスト・ルートPrimeFacesSample062-Download

コンテンツ・ディレクトリーWebContent
web.xml デプロイメント記述子の作成チェックあり


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://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <display-name>PrimeFacesSample062-Download</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>

3.「Ctrl+Shift+F」を押し、ソースコードをフォーマッティング、「Ctrl+S」でファイルを保存。
4.「任意のファイルの作成方法」の手順で、「WebContent/」ディレクトリに「index.xhtml」というファイルを作成。
5.「index.xhtml」を以下の様に入力。
<html xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
    <h:form id="form">

        <p:commandLink value="ダウンロード" ajax="false">
            <p:fileDownload value="#{downloadBean.file}" />
        </p:commandLink>

        <p:commandButton value="ダウンロード" ajax="false">
            <p:fileDownload value="#{downloadBean.file}" />
        </p:commandButton>
    </h:form>
</h:body>
</html>

6.「Ctrl+Shift+F」を押し、ソースコードをフォーマッティング、「Ctrl+S」でファイルを保存。
7.「Javaパッケージの作成方法」の手順で、「com.example.primefacessample062」というパッケージを作成。
8.「Javaクラスファイルの作成方法」の手順で、「DownloadBean.java」というクラスファイルを作成。
9.「DownloadBean.java」を以下の様に入力。
package com.example.primefacessample062;

import java.io.InputStream;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

@ManagedBean
@RequestScoped
public class DownloadBean implements Serializable {
    private StreamedContent file;

    public DownloadBean() {
        InputStream stream = ((ServletContext) FacesContext
                .getCurrentInstance().getExternalContext().getContext())
                .getResourceAsStream("/sample.jpg");
        file = new DefaultStreamedContent(stream, "image/jpg",
                "sample_download.jpg");
    }

    public StreamedContent getFile() {
        return file;
    }
}

10.「WebContent」ディレクトリ直下に「sample.jpg」の画像ファイルを配置しておきます。
11.「動的Webアプリケーションをサーバーに配置する方法」の手順で、作成したプロジェクトをサーバーに配置します。
12.「サーバーをデバッグモードで起動する方法」の手順で、サーバーを起動します。
13.ブラウザで以下のURLにアクセスします。
http://localhost:8080/PrimeFacesSample062-Download/faces/index.xhtml

14.以下の様に表示されます。
15.「ダウンロード」ボタンをクリック。





16.ダウンロードが開始されれば成功です。




























以上です。

0 件のコメント:

コメントを投稿

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

関連記事