かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[Java][NetBeans]EJB3.0って簡単なの?

さて、EJB3.0が出てから結構たつけど、ほとんどやったことがない!
ということで、NetBeans6.1 + GlassFish V2という開発環境と実行環境も手元にあることだしやってみよう。

EJB3Eduという名前でエンタープライズアプリケーションを新規作成する。
そうすると、EJB3Edu-ejbというプロジェクトと、EJB3Edu-warというプロジェクトも一緒に作られる。
ejbのほうにセッションビーンとかを置くみたい。warの方には、もちろんWeb周りを作っていくことになる。

JavaのWebアプリ開発は、素のJSP/Servletとかではやる気がでないのでVisual Web JSFを使うことにする。
プロジェクトのプロパティからフレームワークを選択してVisual Web JavaServer Facesを追加する。
ここら辺、Webフレームワークとして統合されたおかげで、かなり柔軟になった。

最後に、Webのプロジェクトを自分色にしやすくするために、生成されたjspやApplicationBean SessionBean等を削除する。ソースパッケージにはBundle.propertiesが寂しく残るだけになればOK。

Statelessなセッションビーンを作ってみるか

EJBのプロジェクトのソースパッケージにcom.wankuma.kazuki.ejbというパッケージを作っておく。
そして、新規作成からセッション Beanを作成する。

EJB名をCalc。パッケージをcom.wankuma,kazuki.ejbにしてステートレスのローカルを選んでおく。
そうすると、セッションビーンの雛形が作られる

// CalcLocal.java
package com.wankuma.kazuki.ejb;

import javax.ejb.Local;

@Local
public interface CalcLocal {
    
}

// CalcBean.java
package com.wankuma.kazuki.ejb;

import javax.ejb.Stateless;

@Stateless
public class CalcBean implements CalcLocal {
    // Add business logic below. (Right-click in editor and choose
    // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
}

コメントを見ると、右クリックからEJBメソッドのビジネスメソッドを追加あたりを選べみたいなことが書いてあるので素直に従う。
そうすると、下のようにメソッドを追加するダイアログが出てくるので適当に入力する。
ここでは、足し算をするだけのものを作ろうとしてるので、下のように入力。
image

public int calc(int lhs, int rhs)みたいな感じの出来る。
次にEJB3からはテストしやすいぜ!ってのがウリみたいなので、JUnitのテストを書いてから、実装してみようと思う。
JUnitの生成もIDEからさくっとできる。対象のJavaファイルの右クリックメニューからツールのJUnitテストを作成。
image

自動で吐き出されるコードをベースに足し算のテストを書いてみる。

package com.wankuma.kazuki.ejb;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Kazuki
 */
public class CalcBeanTest {

    private CalcBean bean;
    
    @Before
    public void setUp() {
        bean = new CalcBean();
    }

    @After
    public void tearDown() {
        bean = null;
    }

    @Test
    public void testAdd() {
        assertEquals(2, bean.add(1, 1));
    }

}

実行すると当然失敗!下にように怒られる。
image

失敗を確認したので、実装!まぁ、JUnitでテスト書くまでもないようなロジックだけどなんとなくね。

@Stateless
public class CalcBean implements CalcLocal {

    public int add(final int lhs, final int rhs) {
        return lhs + rhs;
    }
    
}

無事テストも成功。
image

画面も作るよ!

image

作った!!!
ぽとぺたで画面が作れるのはやっぱりいい。
テキストフィールドにはIntegerコンバータをしかけて数値としてデータが取れるようにしている。

次に、Javaのコード側にセッションビーンのインターフェイスをフィールドとして定義する。
そして、getter/setterを定義してsetterにEJBアノテーションを追加する。

    private CalcLocal calc;
    public CalcLocal getCalc() {
        return calc;
    }
    @EJB
    public void setCalc(CalcLocal calc) {
        this.calc = calc;
    }

これで実行時に自動的にインスタンスが設定されるはずだ。
次に、テキストボックスからの入力を受け取るプロパティや、計算結果のためのプロパティを定義する。

    private int lhs; // 左辺値
    private int rhs; // 右辺値
    private int answer; // 答え

    // getter/setterは省略

そして、このプロパティを画面のコンポーネントとバインドする。バインドもGUIからさくっと指定できる。
image
JSP上で↓のような感じになるようにバインドを指定する。

                            <webuijsf:textField converter="#{CalcPage.integerConverter1}" id="textFieldLhs" required="true" text="#{CalcPage.lhs}"/>
                            <webuijsf:staticText id="staticText2" style="" text="+"/>
                            <webuijsf:textField converter="#{CalcPage.integerConverter1}" id="textFieldRhs" required="true" text="#{CalcPage.rhs}"/>
                            <webuijsf:staticText id="staticText3" text="="/>
                            <webuijsf:staticText id="staticTextAnswer" text="#{CalcPage.answer}"/>

後は、ボタンにさっき作ったaddメソッドを呼ぶコードを書けばOK。

    public String button1_action() {
        answer = calc.add(lhs, rhs);
        return null;
    }

これで完成~。

動作確認!

実行すると下のような画面になる。数字を入力してボタンを押すとちゃんと計算もできる。
image image

感想

思ったより簡単に足し算が出来た。
これは結構いいかもしれん。

投稿日時 : 2008年5月28日 22:33

Feedback

# re: [Java][NetBeans]EJB3.0って簡単なの? 2008/05/28 23:46 凪瀬

ふむー。EJB3.0は2.0の苦い教訓があって手を出していないんだけど、随分楽になっていると聞くし、ちゃんと再評価しないといけないなぁ。

# [Java][NetBeans]EJB3って簡単なの? その2 2008/05/30 0:04 かずきのBlog

[Java][NetBeans]EJB3って簡単なの? その2

# re: [Java][NetBeans]EJB3.0って簡単なの? 2008/05/30 0:05 かずき

>凪瀬さん
NetBeans6.1を開発環境にしてEJB3は結構よさげな雰囲気ですよ~。

# GEyCKTnNcPZiFUfv 2011/09/28 22:49 http://oemfinder.com

jGfhjO Heartfelt thanks..!

# gZoiEdCXNzfmNwFdhuG 2011/11/02 5:24 http://www.pharmaciecambier.com/

I do`t see a feedback or the other coordinates from the blog administration!...

# XQmHIuIWZGpwXhg 2011/11/02 6:19 http://optclinic.com/

Can be also this issue because the truth can be achieved only in a dispute :D

# pFjByYHlnBcMfNs 2011/11/15 3:59 http://www.pharmaciedelange.com/

Author, keep doing in the same way..!

# iMpAAPqNKtB 2011/11/16 3:55 http://www.laurenslinens.com/boysbedding.html

Of course, I understand a little about this post but will try cope with it!!...

# full welded ball valve 2012/10/18 20:29 http://www.dwkvalve.com/product_cat_list/Full-Weld

of course like your web site however you have to check the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to inform the truth nevertheless I will surely come back again.

# lancel 2012/10/19 14:54 http://www.saclancelpascher2013.com

I really like your writing style, great info, appreciate it for posting :D. "I will show you fear in a handful of dust." by T. S. Eliot.

# burberry scarf sale 2012/10/26 3:39 http://www.burberryoutletscarfsale.com

hi!,I really like your writing very much! share we keep in touch extra about your article on AOL? I need an expert in this space to unravel my problem. Maybe that is you! Taking a look ahead to see you.
burberry scarf sale http://www.burberryoutletscarfsale.com

# wallet 2012/10/26 3:39 http://www.burberryoutletscarfsale.com/accessories

Appreciate it for helping out, fantastic information.
wallet http://www.burberryoutletscarfsale.com/accessories/burberry-wallets-2012.html

# t shirts 2012/10/26 3:40 http://www.burberryoutletscarfsale.com/burberry-wo

Only wanna input on few general things, The website pattern is perfect, the articles is very great. "The sun sets without thy assistance." by The Talmud.
t shirts http://www.burberryoutletscarfsale.com/burberry-womens-shirts.html

# cheap burberry bags 2012/10/26 3:40 http://www.burberryoutletscarfsale.com/burberry-ba

I got what you mean , regards for putting up.Woh I am lucky to find this website through google. "The test and use of a man's education is that he finds pleasure in the exercise of his mind." by Carl Barzun.
cheap burberry bags http://www.burberryoutletscarfsale.com/burberry-bags.html

# burberry mens shirts 2012/10/26 3:40 http://www.burberryoutletscarfsale.com/burberry-me

I really like your writing style, superb information, regards for putting up :D. "If a cluttered desk is the sign of a cluttered mind, what is the significance of a clean desk" by Laurence J. Peter.
burberry mens shirts http://www.burberryoutletscarfsale.com/burberry-men-shirts.html

# cheap louis vuitton purses 2012/10/28 0:32 http://www.louisvuittonoutletbags2013.com/

Glee is the perfume you should not strain concerned with other consumers not including purchasing a number droplets concerned with that you are.
cheap louis vuitton purses http://www.louisvuittonoutletbags2013.com/

# louis vuitton wallet 2012/10/28 0:42 http://www.louisvuittonwallets2013.com/

Probably Lord desires us to fulfill various bad people today previous to getting together the most appropriate one, with the intention that weight lifting does not definitely fulfill the particular person, we're going to can be happier.
louis vuitton wallet http://www.louisvuittonwallets2013.com/

# louis vuitton speedy 2012/10/28 0:46 http://www.louisvuittonoutletdiaperbag.com/

The particular unappealing approach neglect another person shall be relaxing suitable invariably these products realizing you can easlily‘big t make them.
louis vuitton speedy http://www.louisvuittonoutletdiaperbag.com/

# burberry mens shirts 2012/10/28 16:02 http://www.burberryoutletonlineshopping.com/burber

Somebody essentially lend a hand to make significantly articles I'd state. This is the first time I frequented your website page and to this point? I surprised with the research you made to create this actual put up amazing. Wonderful process!
burberry mens shirts http://www.burberryoutletonlineshopping.com/burberry-men-shirts.html

# burberry wallets 2012/10/28 16:02 http://www.burberryoutletonlineshopping.com/burber

Hello, Neat post. There is a problem together with your web site in internet explorer, would check this… IE nonetheless is the market chief and a good element of people will omit your fantastic writing because of this problem.
burberry wallets http://www.burberryoutletonlineshopping.com/burberry-wallets-2012.html

# burberry womens shirts 2012/10/28 16:02 http://www.burberryoutletonlineshopping.com/burber

Absolutely pent content material, thanks for information. "The last time I saw him he was walking down Lover's Lane holding his own hand." by Fred Allen.
burberry womens shirts http://www.burberryoutletonlineshopping.com/burberry-womens-shirts.html

# Burberry Watches 2012/10/28 16:05 http://www.burberryoutletscarfsale.com/accessories

Some really prize posts on this web site , saved to fav.
Burberry Watches http://www.burberryoutletscarfsale.com/accessories/burberry-watches.html

# scarf 2012/10/28 16:05 http://www.burberryoutletscarfsale.com/accessories

Hello, Neat post. There is a problem with your web site in internet explorer, would test this… IE nonetheless is the marketplace chief and a good section of people will pass over your wonderful writing because of this problem.
scarf http://www.burberryoutletscarfsale.com/accessories/burberry-scarf.html

# EAuaJGWoWyTpeo 2014/08/28 1:22 http://crorkz.com/

msva3b Greetings! I've been reading your website for a long time now and finally got the courage to go ahead and give you a shout out from Houston Tx! Just wanted to mention keep up the fantastic job!

# KuWdGqtANyPysuFAp 2014/09/18 16:36 http://huntingdogbreeders.info/story/28955

GQHIIH Really enjoyed this blog.Really looking forward to read more. Awesome.

タイトル
名前
Url
コメント