LoginSignup
1
1

More than 5 years have passed since last update.

LaBee Framework 任意レスポンス設定(ファイルダウンロード)、アップロード方法

Posted at

LaBee FrameworkはJavaによるWebシステム開発のデファクトスタンダードを目指す、ゼロから作られた国産のJavaフレームワークです。 海外製フレームワーク特有の難解さや情報不足による工数や人員の増大を解消しJavaのWeb開発を効率化する為に作られました。 LGPLライセンスでソースコードをオープンソース公開しており、個人・企業問わずどなたでも無償で利用出来ます
https://www.bee-wkspace.com/
press.jpg

ビジネスロジック任意レスポンス

ビジネスロジックの処理が終了した際のレスポンス設定メソッドを親クラスで複数定義しています。レスポンス設定を変更する事で別のビジネスロジックにリダイレクトしたりレスポンスを委譲するなどを簡単に実装出来ます。

ビジネスロジックのレスポンス設定種類

レスポンスタイプ 概要
通常 responseContextに何も設定を行なわない場合はデフォルトでビジネスロジックと同じ画面IDのJSPへ遷移します。
setRedirectResponse(String target, String execute, String... param) 指定したターゲットのイベント処理へのリダイレクト呼び出しを行ないます。
setRedirectURL(String redirectURL, String statusCode, HashMap headerMap) 指定したURLへのリダイレクト呼び出しを行ないます。
setDelegateResponse(String target, String execute, String... params) 指定したターゲットのイベント処理を直列実行しレスポンスを委譲します。委譲回数に制限はなく直列に複数の ビジネスロジックを実行出来ます。
setAjaxJsonResponse(JsonValueContext jsonvalContext) 「ID名,設定値」形式のJSONをレスポンスを返し、ビジネスロジックと同じ画面IDのJSPへ遷移し、指定IDのオブジェクト内容を 動的に書き換えます。
setOtherScreenIdResponse(String screenId) 他の画面IDのJSPへ遷移するレスポンス設定を行なう。遷移先jspではビーン参照クラスを合わせるか、ビーン参照を使用しない 必要がある。

ファイルダウンロード

ビジネスロジックの処理が終了した際のレスポンス情報を持つresponseContextを設定する事でレスポンスにファイルを返す(ファイルダウンロード)事が出来ます。ファイル種別はプレーンテキスト、CSV、JSON、XML、バイナリに対応しています。

JSONをダウンロードする Blogicメソッド実装例

    /**
     * JSONをダウンロードするイベント処理。
     */
    @FwExeMethod
    public ResponseContext jsonDownload() throws FwException {
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("Language", "Java");
            List<String> dataList = new ArrayList<String>();
            dataList.add("Windows");
            dataList.add("Mac");
            dataList.add("Linux");

            dataMap.put("Enviroment", dataList);

            JsonUtil<Map<String, Object>> jsonUtil = new JsonUtil<Map<String, Object>>();
            String json = jsonUtil.encodeToString(dataMap);
            responseContext.setExportFileName("Sample.json");
            responseContext.setResponseType(ResponseContext.RESPONSE_TYPE_JSON);
            responseContext.setResponseJson(json);
        } catch (Exception e) {
            throw new FwException(e);
        }
        return responseContext;
    }

CSVをダウンロードする Blogicメソッド実装例

    /**
     * CSVをダウンロードするイベント処理。
     */
    @FwExeMethod
    public ResponseContext jsonDownload() throws FwException {
        try {
            StringBuilder csv = new StringBuilder();
            csv.append("北海道");
            csv.append(CommonDefine.COMMA);
            csv.append("青森");
            csv.append(CommonDefine.COMMA);
            csv.append("秋田");
            csv.append(CommonDefine.COMMA);
            csv.append("岩手");

            responseContext.setExportFileName("Sample.csv");
            responseContext.setResponseType(ResponseContext.RESPONSE_TYPE_CSV);
            responseContext.setResponseCsv(csv.toString()); 
        } catch (Exception e) {
            throw new FwException(e);
        }
        return responseContext;
    }

バイナリをダウンロードする Blogicメソッド実装例


    /**
     * バイナリをダウンロードするイベント処理。
     */
    @FwExeMethod
    public ResponseContext jsonDownload() throws FwException {
        try {
            byte[] binary = new String("バイナリテスト").getBytes();
            responseContext.setExportFileName("Sample.bin");
            responseContext.setResponseType(ResponseContext.RESPONSE_TYPE_BINARY);
            responseContext.setResponseBinary(binary);
        } catch (Exception e) {
            throw new FwException(e);
        }
        return responseContext;
    }

ファイルアップロード

LaBeeFrameworkは簡単にWeb画面からファイルアップロードし、ビジネスロジックで取得する事が出来ます。ほとんど特別な処理は不要です。サンプル例ではWeb画面(JSP)からファイル選択後にサブミットし、ビジネスロジックで取得したファイル内容をビーンに詰めてJSPにレスポンスを返しています。

JSP実装例


<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ page session="false"%>
<%@ taglib prefix="LaBee"  uri="/WEB-INF/lib/LaBeeFramework.jar"%>
<jsp:useBean id="bean" class="sample.bean.web.SampleBean" scope="request" />
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <LaBee:header requestScope="${requestScope}"/>
</head>
<body>
    <form name="mainForm" method="post" action="ctrl">
        <input type="file" name="file">
        <br>    

        <button type="button"
            onClick="<LaBee:postMultiPartSubmit
            target="web.Sample"
            execute="upload" 
            formName="mainForm"/>">
            ファイル送信
         </button>          

         <div>
            ${bean.uploadFileName}
         </div>
         <div>
            ${bean.uploadText}
         </div>
    </form>
</body>
</html>         
  • 16行目 ファイル選択タグを設置します。
  • 20行目 ボタン押下時にLaBee:postMultiPartSubmitカスタムタグでマルチパート送信します。

Bean実装例

package sample.bean.web;
import java.io.Serializable;
import com.bee_wkspace.labee_fw.app.base.AppBaseBean;
/**
 * ファイルアップロードサンプルビーンクラス
 */
public class SampleBean extends AppBaseBean implements Serializable {
    /** アップロードファイルテキスト */
    private String uploadText;
    /** アップロードファイル名 */
    private String uploadFileName;
    /**
     * コンストラクタ。
     */
    public SampleBean() {
        super();
    }
    /**
     * @return uploadText
     */
    public String getUploadText() {
        return uploadText;
    }
    /**
     * @return uploadFileName
     */
    public String getUploadFileName() {
        return uploadFileName;
    }
    /**
     * @param uploadText セットする uploadText
     */
    public void setUploadText(String uploadText) {
        this.uploadText = uploadText;
    }
    /**
     * @param uploadFileName セットする uploadFileName
     */
    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
}       

Blogic実装例


    package sample.blogic.web;
    import sample.bean.web.SampleBean;
    import com.bee_wkspace.labee_fw.app.base.AppBaseBlogic;
    import com.bee_wkspace.labee_fw.core.annotation.FwBlogic;
    import com.bee_wkspace.labee_fw.core.annotation.FwExeMethod;
    import com.bee_wkspace.labee_fw.core.context.ResponseContext;
    import com.bee_wkspace.labee_fw.exception.FwException;
    /**
     * ファイルアップロードサンプルビジネスロジック
     */
    @FwBlogic(beanReuse = false)
    public class SampleBlogic extends AppBaseBlogic<SampleBean> {
        /**
         * コンストラクタ。
         */
        public SampleBlogic() {
            super();
        }
        /**
         * ファイルアップロードイベント処理。
         */
        @FwExeMethod
        public ResponseContext upload() throws FwException {
            try {
                // 受信したファイル内容をビーンに格納
                bean.setUploadFileName(super.uploadFileContext.getFileName());
                bean.setUploadText(super.uploadFileContext.getUploadString());

            } catch (Exception e) {
                throw new FwException(e);
            }
            return responseContext;
        }
    }

  • 31行目~32行目 アップロードされたファイル情報をビーンに格納しています。アップロードされたファイル情報は親クラスて定義しているcom.bee_wkspace.labee_fw.core.context.FileUploadContextクラス型の uploadFileContextインスタンスに自動的に格納されますので実装者は何もせずにアップロードファイル情報を取得出来ます。

FileUploadContexクラス概要

メソッド名 概要
String getFileName() アップロードしたファイルのファイル名を返します。
byte[] getUploadFileByte() アップロードしたファイルのバイナリバイト配列を返します。
String getUploadString() アップロードしたファイルを文字列に変換して返します。
int getFileSize() アップロードしたファイルのファイルサイズを返します。
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1