忍者ブログ
まにょのITメモ
[46]  [45]  [44]  [39]  [43]  [42]  [41]  [40]  [38]  [37]  [36
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

動的に内容が変わるセレクトボックスの作り方。


***bean***
セレクトボックスのためだけのbeanを作る。

public  class  UserName{

    private  String  name;

    private  String  label;

    public  String  getName() {
        return  name;
     }

    public  void  setName(String name) {
        this.name  =  name;
    }

    public  String  getLabel() {
        return  label;
    }

    public  void  setLabel(String label) {
        this.label  =  label;
    }



***Action***
beanに値をセットしてListにつめる。

List<UserName>  list  =  new  ArrayList<UserName>();

  UserName  un1  =  new  UserName();
  un1.setLabel("山田");
  un1.setName("01");
  list.add(un1);

  UserName  un2  =  new  UserName();
  un2 .setLabel("田中");
  un2 .setName("02");
  list.add(un2 );

  req.setAttribute("userName ",  list);


------
※注
実はstrutsで用意されている org.apache.struts.util.LabelValueBean という
クラスがあって、それを使うと自分でBeanを作らなくていいんですよね。すごい便利。

使い方↓
とりあえずまずimportしておく
import org.apache.struts.util.LabelValueBean;

    List<UserName>  list  =  new  ArrayList<UserName>();

    new   LabelValueBean("Lavel(画面のセレクトボックスに表示される値)", "value(裏で持つ値)");
    LabelValueBean  lvb1  =  new   LabelValueBean("山田", "01");
    LabelValueBean  lvb2  =  new   LabelValueBean("田中", "02");

    list.add(lbv1);
    list.add(lbv2);

  req.setAttribute("userName ",  list);
------


***jsp***

<html:select property="memberName">
       <html:options collection="userName " property="name" labelProperty="label"/>
</html:select>

------
※注
org.apache.struts.util.LabelValueBean を使ったとき

<html:select property="birthYear">
       <html:options collection="userName " property="value" labelProperty="label"/>
</html:select>
------

collection="setAttributeでつめたキー"
property="裏で持つ値"
labelProperty="画面のセレクトボックスに表示される値"


 

PR
この記事にコメントする
お名前
タイトル
文字色
メールアドレス
URL
コメント
パスワード   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
この記事へのトラックバック
この記事にトラックバックする:
この記事のURL:
忍者ブログ * [PR]