Quantcast
Channel: normalian blog
Viewing all articles
Browse latest Browse all 237

Microsoft Azure の Web サイトで CDI と JSF を動かしてみる

$
0
0

JavaEE の実行環境を Microsoft Azure の Web サイト上で稼働させるで TomcatEE が Web サイト上で動作することを紹介できたが、今回は実際に JSFCDIを利用したアプリケーションが動くかを検証する。前回の記事でも記載しているが、TomcatEE における JavaEEの実装は以下になる。

上記の内容は アノテーションで大混乱した JavaEE6 頃の実装だが、めげずにアプリを作成したいと思う。また、今回利用したアプリは https://github.com/normalian/MavenWebAppに配置しているので、別途参照してほしい。

アプリケーションの概要

今回作成したアプリケーションは、以下の様に JSFでのデータバインディングを行い、セッションにデータを格納して次の画面で値を表示する簡単な内容だ。
f:id:waritohutsu:20150104152517p:plain

コードの詳細は GitHubを参照いただくとして、トップ画面の XHTML, Javaコードを紹介する。

<?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"><h:head><title>Facelet Title</title></h:head><h:body><h:form>
            Hello from Facelets &amp; Please input message
            <h:inputText value="#{viewDto.message}" /><br /><h:commandButton value="submit"action="#{indexAction.doAction}" /></h:form></h:body></html>
package com.mycompany.mavenwebapp.common;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@SessionScoped@Namedpublicclass ViewDto implements Serializable {

    private String message;

    public String getMessage() {
        return message;
    }

    publicvoid setMessage(String message) {
        this.message = message;
    }
}
package com.mycompany.mavenwebapp.action;

import com.mycompany.mavenwebapp.common.ViewDto;
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named@RequestScopedpublicclass IndexAction implements Serializable {

    @Inject
    ViewDto viewDto;

    public String doAction() {
        System.out.println("@@@@@@@@ message = " + viewDto.getMessage());
        return"show.xhtml";
    }
}

ご覧のとおり、JSFCDIを利用した簡単なアプリケーションだ。ローカルの TomcatEE でも動作を確認後、Kudu を利用して D:\home\site\wwwroot\bin\apache-tomee-plus-1.7.1\webapps 以下に war ファイルを配置する。war 配置後は冒頭で記載した様に JSFCDIが正しく動作していることが確認できる。

異常で、Jetty や Tomcatしか動かないと思われた Web サイトでも JavaEEの機能が利用できることが紹介できた。是非皆様もいろんなハックを Web サイト上でやってみよう!


Viewing all articles
Browse latest Browse all 237

Trending Articles