[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="画面のセレクトボックスに表示される値"
~ラベルで表示している文字をフォームに入れて次に渡したい時~
<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"/>
の違う書き方。
これでも良い。
★Mathクラスのメソッド
abs ・・・引数の絶対値を返す
ceil ・・・引数の値以上で最小の整数を返す
floor ・・・引数の値以下で最大の整数を返す
public class SampleMath {
public static void main(String[] args) {
double d = 100;
System.out.println("abs : " + Math.abs(d));
System.out.println("ceil : " + Math.ceil(d));
System.out.println("floor : " + Math.floor(d));
}
}
~実行結果~
abs : 100.0
ceil : 100.0
floor : 100.0
--------------------------------------------------------------------
public class SampleMath {
public static void main(String[] args) {
double d = 100.1;
System.out.println("abs : " + Math.abs(d));
System.out.println("ceil : " + Math.ceil(d));
System.out.println("floor : " + Math.floor(d));
}
}
~実行結果~
abs : 100.1
ceil : 101.0
floor : 100.0
--------------------------------------------------------------------
public class SampleMath {
public static void main(String[] args) {
double d = -100.1;
System.out.println("abs : " + Math.abs(d));
System.out.println("ceil : " + Math.ceil(d));
System.out.println("floor : " + Math.floor(d));
}
}
~実行結果~
abs : 100.1
ceil : -100.0
floor : -101.0
public static void main(String[] args) {
try {
return;
}finally {
System.out.println("finally");
}
}
-----------------------
実行結果
finally
try{ }の中でreturnをしても、finallyブロックは必ず実行される。
public class Operando {
public static void main(String[] args) {
int i = 1;
if((i == 0)&&(i++ == 1)) {
System.out.println("true : " + i);
}else {
System.out.println("false : " + i);
}
int n = 1;
if((n == 0)&(n++ == 1)) {
System.out.println("true : " + n);
}else {
System.out.println("false : " + n);
}
int t = 1;
if((t == 1)||(t++ == 1)) {
System.out.println("true : " + t);
}else {
System.out.println("false : " + t);
}
int x = 1;
if((x == 1)|(x++ == 1)) {
System.out.println("true : " + x);
}else {
System.out.println("false : " + x);
}
}
}
実行結果
false : 1
false : 2
true : 1
true : 2
&& や || は、まず左オペランドの比較を見ます。
左オペランドだけで結果を返せる場合は右オペランドを見ません。
& や | は左オペランドを見て、その後、右オペランドも必ず見ます。
右オペランドにインクリメントなどがある場合は気をつけましょう。