Struts 2 コンボボックスの例
ダウンロードする – リンク://wp-content/uploads/2010/06/Struts-ComboBox-Example.zip[Struts-ComboBox-Example.zip]
Struts 2では、
<s:combobox>
タグは基本的に1行のテキストボックス** とグループ化されたドロップダウンリストで、ユーザーがテキストボックスに値を直接入力するか、ドロップダウンリストをクリックすると、選択した値がテキストボックスに自動的に入力されます。
ドロップダウンリストとコンボボックスのリストの間に混乱がある場合は、http://ja.wikipedia.org/wiki/Combo__box[comboボックス定義をWikiから読んでください]。
<s:combobox label="What's your favor fruit"
headerKey="-1" headerValue="--- Select ---"
list="fruits"
name="yourFruits"/>
その結果、次のHTMLコード…
<td class="tdLabel">
<label for="resultAction__yourFruits" class="label">
What's your favor fruit:
</label>
</td>
<td>
<script type="text/javascript">
function autoPopulate__resultAction__yourFruits(targetElement) {
if (targetElement.options[targetElement.selectedIndex].value == '-1') {
return;
}
targetElement.form.elements['yourFruits'].value=
targetElement.options[targetElement.selectedIndex].value;
}
</script>
<input type="text" name="yourFruits" value="" id="resultAction__yourFruits"/>
<br/>
<select onChange="autoPopulate__resultAction__yourFruits(this);">
<option value="-1">--- Select ---</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Orange">Orange</option>
<option value="Watermelon">Watermelon</option>
</select>
</td>
ドロップダウンリストを作成するには、代わりに
<s:select>
タグを使用する必要があります。
Struts 2
<s:combobox>
例
-
<s:combobox> ** でコンボボックスを使用する完全なStruts 2の例
1.アクション
選択されたコンボボックスオプションを生成して保持するActionクラス。
ComboBoxAction.java
package com.mkyong.common.action;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class ComboBoxAction extends ActionSupport{
private List<String> fruits;
private String yourFruits;
private String yourMonth;
public String getYourMonth() {
return yourMonth;
}
public void setYourMonth(String yourMonth) {
this.yourMonth = yourMonth;
}
public List<String> getFruits() {
return fruits;
}
public void setFruits(List<String> fruits) {
this.fruits = fruits;
}
public String getYourFruits() {
return yourFruits;
}
public void setYourFruits(String yourFruits) {
this.yourFruits = yourFruits;
}
public ComboBoxAction(){
fruits = new ArrayList<String>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Watermelon");
}
public String execute() {
return SUCCESS;
}
public String display() {
return NONE;
}
}
2.結果ページ
”
<s:combobox>
“タグを使用してコンボボックスをレンダリングし、JavaリストおよびOGNLリストを使用して選択オプションを設定します
-
combobox.jsp **
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>Struts 2 <s:combobox> example</h1>
<s:form action="resultAction" namespace="/">
<h2>
<s:combobox label="What's your favor fruit"
headerKey="-1" headerValue="--- Select ---"
list="fruits"
name="yourFruits"/>
</h2>
<h2>
<s:combobox label="Select a month"
headerKey="-1" headerValue="--- Select ---"
list="#{'1':'Jan', '2':'Feb', '3':'Mar', '4':'Apr'}"
name="yourMonth"/>
</h2>
<s:submit value="submit" name="submit"/>
</s:form>
</body>
</html>
-
result.jsp **
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 <s:combobox> example</h1> <h2> Favor fruit : <s:property value="yourFruits"/> </h2> <h2> Selected month : <s:property value="yourMonth"/> </h2> </body> </html>
3. struts.xml
すべて一緒にリンクする〜
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="comboBoxAction"
class="com.mkyong.common.action.ComboBoxAction" method="display">
<result name="none">pages/combobox.jsp</result>
</action>
<action name="resultAction" class="com.mkyong.common.action.ComboBoxAction">
<result name="success">pages/result.jsp</result>
</action>
</package>
</struts>
5.デモ

