忍者ブログ
まにょのITメモ
[3]  [4]  [5]  [6]  [7]  [8]  [9]  [10]  [11]  [12]  [13
×

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

String  str  =  "abc";
str  =  str  +  "123";


元の  str  =  "abc"  に"123"を足して str  = "abc123" となっているように見えるが、
str  =  str  +  "123";
を実行した時に、新しいString型オブジェクトが作られている。

String str  =  "abc123";
と新たに宣言しているのと同じ。

String型は変更を加える度に、新しいオブジェクトが作られている。
PR

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


***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="画面のセレクトボックスに表示される値"


 

~ラベルで表示している文字をフォームに入れて次に渡したい時~

<bean:write name="userRegisterForm" property="userId"/>

userRegisterForm というフォームから getUserId して取得できた値を bean:writeで表示している。
この状態だと、画面に表示はできているが、次のアクションに渡すフォームは空のままになっている。
(フォームに値をつめる、という作業をしていないから。)


そんな時はこれ↓

<html:hidden name="userRegisterForm" property="userId"/>

画面の裏で、こっそり hidden でフォームに値を持たせておく。
name="データを詰めたいフォームの名前"   property="フォームの持つフィールドの名前"/


<bean:write name="userRegisterForm" property="userId"/>
で画面に表示させておいて
<html:hidden name="userRegisterForm" property="userId"/>
でこっそりフォームに値をもたせておくのだ。

----------------------------------------------------------------------------------------

<input type="hidden" name="userId" value="<bean:write name="userRegisterForm" property="userId"/>">

<html:hidden name="userRegisterForm" property="userId"/>
の違う書き方。
これでも良い。


 

<action-mappings>

    <action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="loginForm" input="/pages/login.jsp" scope="session">

        <forward name="success" path="/showCom.do"></forward>

        <forward name="fail" path="/pages/login.jsp"></forward>

    </action>

</action-mappings>


forward  pathのところに遷移画面のjspを書くのではなくて
"/showCom.do"
のように  アクション名.do  と書くと
successしたあとに、指定したアクションを実行する
コンストラクタ・・・クラス名と同じ名前で、戻り値を持たないメソッド。

デフォルトコンストラクタ・・・引数のないコンストラクタ。クラスの中に明示的にコンストラクタを書かないと、コンパイル時に自動的に作られる。引数があるコンストラクタをひとつでも書いていたら、デフォルトコンストラクタが自動的に作られることはない。
忍者ブログ * [PR]