Struts 2 <s:combobox>コンボボックスの例

Struts 2 コンボボックスの例

ダウンロード–Struts-ComboBox-Example.zip

Struts 2では、<s:combobox>タグは基本的にdrop down list grouped together with a single-line text boxであり、ユーザーはテキストボックスに直接値を入力するか、ドロップダウンリストから値を選択できます。選択した値は次のように入力されます。テキストボックスが自動的に表示されます。

ドロップダウンリストとコンボボックスリストを混同している場合は、combo box definition from Wikiをお読みください。

次のHTMLコードが生成されます…


   





<s:combobox>タグは、入力テキストボックス、「onChange()」動作のドロップダウンリストを生成し、生成されたJavaScript関数を呼び出して、ドロップダウンリストから選択された値を生成されたテキストボックスに自動的に入力します。

ドロップダウンリストを作成するには、代わりに<s:select>タグを使用する必要があります。

Struts 2<s:combobox>の例

<s:combobox>を介したコンボボックスの使用を示す完全なStruts2の例

1. アクション

選択したコンボボックスオプションを生成して保持するアクションクラス。
ComboBoxAction.java

package com.example.common.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class ComboBoxAction extends ActionSupport{

    private List fruits;

    private String yourFruits;
    private String yourMonth;

    public String getYourMonth() {
        return yourMonth;
    }

    public void setYourMonth(String yourMonth) {
        this.yourMonth = yourMonth;
    }

    public List getFruits() {
        return fruits;
    }

    public void setFruits(List fruits) {
        this.fruits = fruits;
    }

    public String getYourFruits() {
        return yourFruits;
    }

    public void setYourFruits(String yourFruits) {
        this.yourFruits = yourFruits;
    }

    public ComboBoxAction(){

        fruits = new ArrayList();
        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" %>





Struts 2 example

result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>



Struts 2 example

Favor fruit :

Selected month :

3. struts.xml

一緒にリンクしてください〜






 



   
    pages/combobox.jsp
   

   
    pages/result.jsp