かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

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

書庫

日記カテゴリ

[Java][NetBeans]日本語版NetBeans6.0.1リリース 皆はVWPでどうやって作ってるんだろう?

サーバの障害等で出るのが送れてたNetBeans6.0.1の日本語版がやっとこさでました。

今まで別で配布されていた、Visual Web PackがNetBeans6から統合されてきたのでガンガン使えたら使っていこうと思います。
でも、結構くせのあるコイツと仲良くやっていくにはどうやるのがいいだろう?
皆はどうやってるんだろう?という疑問を解決するために、小さなサンプルをこさえてみました。

というわけで作るものは、ボタンを押すと表になんかデータが表示されて、表の各行にあるボタンを押すと別画面に遷移してから選択した行のデータが表示されてるっていうものにしました。

早速プロジェクトを作成。SampleWebって名前でルートパッケージはcom.wankuma.kazukiです。
もちろんVisual JavaServer FacesはONにしてね。

新規作成直後の画面は下のような感じ。

image

とりあえず、個人的に最初にすることはSessionBean1とApplicationBean1とRequestBean1をさくっと消す。
Page1でエラーが出るけど気にしないでPage1もさくっと消す。

気を取り直して 新しいページフラグメントを作成する。名前はHeader.jspf。
幅100%で高さ48pxくらいに設定しておこう。StaticTextも置いてタイトルを格好良く作る。

image

ヘッダができたし、Index.jspという名前でページを作る。
ページには、さっき作ったページフラグメントを上部に設置。

ボタンと表も置いていって適当な画面をでっちあげる。
image

この画面に適当なデータを表示してみようと思う。
とりあえず、nameとageを持つPersonクラスを作成!

package com.wankuma.kazuki;

public class Person {
    private int age;
    private String name;
    
    // ここでalt + insert
}

コメントにあるように、alt + insertを押すと下みたいなものがぽろっと出てくる。
矢印キーで取得メソッドおよび設定メソッドを選択するとgetter/setterの生成が可能。

image

image

チェックを入れて生成を押すとさくさくっと出来上がり。
ここらへんお手軽になりました。

次にDataProviderを作ります。ObjectListDataProviderっていうとても使えそうなやつがいるんだけど、画面にぽとっと落としても肝心のObjectTypeプロパティが設定できない。
いつも自分でObjectListDataProviderを継承して、各型向けのDataProviderをこしらえてます。

package com.wankuma.kazuki;

import com.sun.data.provider.impl.ObjectListDataProvider;
import java.util.List;

public class PersonDataProvider extends ObjectListDataProvider {

    public PersonDataProvider(List list) {
        this();
        setList(list);
    }

    public PersonDataProvider() {
        setObjectType(Person.class);
    }
    
}

これを画面のプロパティとして追加します。
Index.javaの適当な位置に下の内容を追加。

    private PersonDataProvider personDataProvider;

    public PersonDataProvider getPersonDataProvider() {
        return personDataProvider;
    }

    public void setPersonDataProvider(PersonDataProvider personDataProvider) {
        this.personDataProvider = personDataProvider;
    }

そしてデザイナに戻るも、さっき追加したプロパティがナビゲータウィンドウに出てこない…。
このバグFIXされたって聞いたんだけどなぁ。おかしい。なおって無い。

デザイナを閉じて開きなおしたら表示された。前はプロジェクトをクリーンビルドしプロジェクト自体を閉じて開きなおさないといけなかったことを考えると大きな進歩かも。

気を取り直して表にpersonDataProviderを関連付ける。
nameとageを表示する列は自動ででるので、ボタン用の列を先頭に追加する。
image

Index.jspのデザイナ画面に置いてあるはずのヘッダ用のページフラグメントが表示されてない…
ここらへんもあやしい。
image

ナビゲータウィンドウを見てみる限り、デザイナ上で表示されてないだけでちゃんとあるっぽいから気にしないでおこう。
次は、検索ボタンをクリックしたときの処理。
本当なら、ここからロジックを受け持つクラスを呼び出す形になるはずだけど、今回は適当なデータをListに突っ込んでDataProviderにセットすることにする。

    public String button1_action() {
        List<Person> people = new ArrayList<Person>();
        for (int i = 0; i < 10; i++) {
            Person p = new Person();
            p.setName("太郎" + i);
            p.setAge(i);
            people.add(p);
        }
        personDataProvider = new PersonDataProvider(people);
        return null;
    }

ここまで出来たら確認のために実行!

404エラーorz
そうだ、Index.jspを開始ページにするのを忘れてた。気を取り直して実行。

実行してボタンを押すと
image 
データが表示される
image
表にあるexecボタンを押すと
image 
悲しいかなデータが消えちゃう。

これは意図した動作なので気にしない。DataProviderはJSFのコンポーネントじゃないのでリクエストを跨ぐと消えてしまう。
ということで自前でどうにかしてとっとかないといけない。
色々考えた結果、destroyでデータをsessionに入れてinitでsessionからデータを取得するようにした。

    @Override
    public void init() {
        ...(略)...       
        // sessionからデータを取得
        personDataProvider = (PersonDataProvider) getValue("#{sessionScope.personDataProvider}");
    }
    @Override
    public void destroy() {
        // sessionにデータを設定
        setValue("#{sessionScope.personDataProvider}", personDataProvider);
    }

これでリクエストを跨いでも表にデータが表示されるようになる。

次に、遷移先画面を作っていく。View.jspという名前で作ってHeader.jspfを置いてGridPanelを置いてcolumnsを2にしてStaticTextを4つ置く。
それらしくプロパティを設定して下のような画面を作る。

image

View.javaの方に移ってPerson型のプロパティpersonを作る。

    private Person person;

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

このPersonプロパティのnameとageをさっき画面に置いたStaticTextにバインドする。

image
image

この画面に表示するPersonオブジェクトは、Index.jspから貰う手はずになってる。(Index.jspのほうにはそういうコード書いて無いけど)
ということで、initにリクエストからPersonオブジェクトを取得するようなコードを書いた。

    @Override
    public void init() {
        ...(略)...
        this.person = (Person) getValue("#{requestScope.person}");
    }

ここまで出来たらあと少し。ページナビゲーションでIndexからViewへの遷移を定義する。
image

Indexの表に置いたボタンのアクションイベントで、リクエストにPersonオブジェクトをつめるだけだ。

    public String button2_action() {
        RowKey rowKey = (RowKey) getValue("#{currentRow.tableRow}");
        setValue("#{requestScope.person}", personDataProvider.getObject(rowKey));
        
        // 画面遷移する前にゴミ掃除
        personDataProvider = null;
        
        return "case1";
    }

これで実行!!

検索ボタン押して…
image
適当な行のexecボタンを押すと…
image 
お~ちゃんと出た~。

作りながら記事書いてたから結構内容ぐちゃぐちゃになってしまった。

投稿日時 : 2008年2月9日 17:36

Feedback

# sac longchamp 2012/10/19 15:33 http://www.sacslongchamppascher2013.com

I truly enjoy studying on this web site, it has fantastic articles. "A short saying oft contains much wisdom." by Sophocles.

# louis vuitton outlet 2012/10/28 3:22 http://www.louisvuittonoutletdiaperbag.com/

Their unattractive technique to forget about somebody shall be working most desirable with these guys knowing you are able to‘p you can keep them.
louis vuitton outlet http://www.louisvuittonoutletdiaperbag.com/

# louis vuitton shoes 2012/10/28 3:23 http://www.louisvuittonwallets2013.com/

A honest friend are you of which overlooks ones outages plus can handle ones positive results.
louis vuitton shoes http://www.louisvuittonwallets2013.com/

# wallet 2012/10/28 14:53 http://www.burberryoutletscarfsale.com/accessories

I went over this web site and I think you have a lot of excellent information, saved to fav (:.
wallet http://www.burberryoutletscarfsale.com/accessories/burberry-wallets-2012.html

# burberry womens shirts 2012/10/28 14:53 http://www.burberryoutletscarfsale.com/burberry-wo

I truly enjoy studying on this site, it holds fantastic posts. "The living is a species of the dead and not a very attractive one." by Friedrich Wilhelm Nietzsche.
burberry womens shirts http://www.burberryoutletscarfsale.com/burberry-womens-shirts.html

# Burberry Ties 2012/10/28 14:53 http://www.burberryoutletscarfsale.com/accessories

Thankyou for helping out, great information.
Burberry Ties http://www.burberryoutletscarfsale.com/accessories/burberry-ties.html

# burberry scarf 2012/10/28 14:53 http://www.burberryoutletscarfsale.com/accessories

I believe this site has some really good info for everyone. "Few friendships would survive if each one knew what his friend says of him behind his back." by Blaise Pascal.
burberry scarf http://www.burberryoutletscarfsale.com/accessories/burberry-scarf.html

# Adidas Climacool Ride 2012/10/30 20:34 http://www.adidasoutle.com/adidas-shoes-adidas-cli

I just could not go away your web site before suggesting that I actually loved the standard info an individual supply to your visitors? Is going to be back steadily to check up on new posts.
Adidas Climacool Ride http://www.adidasoutle.com/adidas-shoes-adidas-climacool-ride-c-1_3.html

# www.supercoatsale.com 2012/10/30 20:35 http://www.supercoatsale.com

Hello, Neat post. There is a problem together with your web site in internet explorer, may test this… IE still is the market leader and a good element of other folks will omit your great writing due to this problem.
www.supercoatsale.com http://www.supercoatsale.com

# Women's Duvetica Coats 2012/10/30 20:35 http://www.supercoatsale.com/canada-goose-duvetica

Thanks for the sensible critique. Me & my neighbor were just preparing to do a little research on this. We got a grab a book from our area library but I think I learned more clear from this post. I'm very glad to see such wonderful information being shared freely out there.
Women's Duvetica Coats http://www.supercoatsale.com/canada-goose-duvetica-womens-duvetica-coats-c-13_16.html

# Women's Duvetica Jackets 2012/10/30 20:36 http://www.supercoatsale.com/canada-goose-duvetica

Somebody necessarily assist to make severely articles I'd state. This is the first time I frequented your web page and up to now? I amazed with the analysis you made to make this actual submit incredible. Fantastic job!
Women's Duvetica Jackets http://www.supercoatsale.com/canada-goose-duvetica-womens-duvetica-coats-c-13_16.html

# Burberry Ties 2012/10/31 20:36 http://www.burberrysalehandbags.com/burberry-ties.

I really like your writing style, wonderful information, thanks for putting up :D. "I will show you fear in a handful of dust." by T. S. Eliot.
Burberry Ties http://www.burberrysalehandbags.com/burberry-ties.html

# burberry bags 2012/10/31 20:36 http://www.burberrysalehandbags.com/burberry-tote-

I really like your writing style, wonderful info, thanks for posting :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 bags http://www.burberrysalehandbags.com/burberry-tote-bags.html

# burberry womens shirts 2012/10/31 20:37 http://www.burberrysalehandbags.com/burberry-women

Dead pent content , thankyou for information .
burberry womens shirts http://www.burberrysalehandbags.com/burberry-womens-shirts.html

# burberry wallets 2012/10/31 20:37 http://www.burberrysalehandbags.com/burberry-walle

Thanks, I've just been searching for information about this topic for ages and yours is the best I've discovered till now. However, what concerning the bottom line? Are you sure concerning the supply?
burberry wallets http://www.burberrysalehandbags.com/burberry-wallets-2012.html

# burberry mens shirts 2012/10/31 20:37 http://www.burberrysalehandbags.com/burberry-men-s

I gotta bookmark this internet site it seems very helpful very helpful
burberry mens shirts http://www.burberrysalehandbags.com/burberry-men-shirts.html

# burberry scarf 2012/11/02 23:10 http://www.burberrysalehandbags.com/burberry-scarf

I dugg some of you post as I cerebrated they were very useful very useful
burberry scarf http://www.burberrysalehandbags.com/burberry-scarf.html

# burberry outlet 2012/11/02 23:21 http://www.burberryoutletonlineshopping.com/

You have brought up a very superb points , regards for the post.
burberry outlet http://www.burberryoutletonlineshopping.com/

# xKWznEPJBE 2014/07/17 12:36 http://crorkz.com/

usV4nA Thanks a lot for the article.Thanks Again. Much obliged.

# It's impressive that you are getting ideas from this piece of writing as well as from our discussion made at this place. 2019/04/24 8:22 It's impressive that you are getting ideas from th

It's impressive that you are getting ideas from this piece of writing as well as
from our discussion made at this place.

# It's really a cool and helpful piece of info. I am satisfied that you simply shared this helpful info with us. Please keep us up to date like this. Thanks for sharing. 2019/07/15 18:07 It's really a cool and helpful piece of info. I am

It's really a cool and helpful piece of info. I am satisfied that you simply shared this helpful info with us.

Please keep us up to date like this. Thanks for sharing.

# It's really a cool and helpful piece of info. I am satisfied that you simply shared this helpful info with us. Please keep us up to date like this. Thanks for sharing. 2019/07/15 18:08 It's really a cool and helpful piece of info. I am

It's really a cool and helpful piece of info. I am satisfied that you simply shared this helpful info with us.

Please keep us up to date like this. Thanks for sharing.

# It's really a cool and helpful piece of info. I am satisfied that you simply shared this helpful info with us. Please keep us up to date like this. Thanks for sharing. 2019/07/15 18:09 It's really a cool and helpful piece of info. I am

It's really a cool and helpful piece of info. I am satisfied that you simply shared this helpful info with us.

Please keep us up to date like this. Thanks for sharing.

# It's really a cool and helpful piece of info. I am satisfied that you simply shared this helpful info with us. Please keep us up to date like this. Thanks for sharing. 2019/07/15 18:10 It's really a cool and helpful piece of info. I am

It's really a cool and helpful piece of info. I am satisfied that you simply shared this helpful info with us.

Please keep us up to date like this. Thanks for sharing.

タイトル
名前
Url
コメント