struts2 doubleselect用法及樣本

來源:互聯網
上載者:User
 1.第一個例子:

<s:select list="{'aa','bb','cc'}" theme="simple" headerKey="00" headerValue="00"></s:select>

2.第二個例子:

<s:select list="#{1:'aa',2:'bb',3:'cc'}" label="abc" listKey="key" listValue="value" headerKey="0" headerValue="aabb">

list 是要顯示的集合,listKey是要顯示的元素id,listValue是要顯示的元素的值,headerKey是預設頭部顯示的值的id,headerValue是預設頭部顯示的元素的值

3.第三個例子:

<%

HashMap map = new LinkedHashMap();

map.put(1,"aaa");map.put(2,"bbb");

map.put(3,"ccc");

request.setAttribute("map",map);

request.setAttribute("aa","2");

%>

<s:select list="#request.map" label="abc"

listKey="key" listValue="value"

value="#request.aa" headerKey="0"

headerValue="aabb"></s:select>

 

<s:select list="discussions" listKey="id" listValue="discussionName" name="seldis" value="%{subject.discussion.id}"></s:select>

這是struts中的select標籤,discussions是List類型對象,id是調用對象中getId方法,value類似,主要是這裡的value值,它採用ognl語言,參數是當前傳過來對象的id值,select標籤會根據這個value值與listvalue值是否相同決定當前條目是否預設顯示。達到一種修改時可以保持原選擇內容的效果。

 

   <s:select label="商品分類" name="categoryParentId"
          id="topList" emptyOption="true" 
          value="%{commodity.commodityCategory.id }"
          list="commoditiesPagination.items" 
          listKey="id"   listValue="categoryName" 
          headerKey="1" headerValue="-商品類型-"
          onchange="categorySelect()"/>

 

=============================================================

doubleselect算是Struts2中稍微複雜點的表單標籤, 尤其官方樣本比較簡單, 沒有太大的實用價值.

 

<s:doubleselect label="doubleselect test2" name="menu" list="#{'fruit':'Nice Fruits', 'other':'Other Dishes'}" doubleName="dishes" doubleList="top == 'fruit' ? {'apple', 'orange'} : {'monkey',
'chicken'}" />

 

從這個官方樣本中可以看到基本用法, list開頭的代表的是第一級下拉框的屬性, doubleList開頭的則是第二級下拉框. 與select標籤的屬性基本是對應的.

 

在實際應用中, 我們比較關注的就是list和doubleList這兩個屬性了, list是存放的第一級下拉框的資料集合, 比如List<FatherBean>, 而doubleList就可以用Map<String, List<SonBean>>來儲存, 其中map的鍵名String與第一級下拉框相關聯.

 

現在來展示一個我寫的樣本:

先寫兩個javaBean,一般情況下是跟資料庫的表相關聯的:

package com.student.bean;

public class Province {
  int id;
  String name;
//set和get方法

}

package com.student.bean;

public class City {
 int id;
 String name;
 int provinceId;
//set和get方法
}

再來寫Action,用來往web頁面發送資料:

package com.student.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.student.bean.Province;
import com.student.bean.City;

public class DoubleSelectAction extends ActionSupport {
    private List<Province> provinceList;
    private Map<Integer, List<City>> cityMap;
   
 public List<Province> getProvinceList() {
  return provinceList;
 }

 public void setProvinceList(List<Province> provinceList) {
  this.provinceList = provinceList;
 }

 public Map<Integer, List<City>> getCityMap() {
  return cityMap;
 }

 public void setCityMap(Map<Integer, List<City>> cityMap) {
  this.cityMap = cityMap;
 }

 public String execute() throws Exception {
      provinceList= new ArrayList<Province>();

            Province province;
            province = new Province();
            province.setId(1);
            province.setName("山西省");
            provinceList.add(province);

            province = new Province();
            province.setId(2);
            province.setName("陝西省");
            provinceList.add(province);

            province = new Province();
            province.setId(3);
            province.setName("河南省");
            provinceList.add(province);

            cityMap = new HashMap<Integer, List<City>>();

            List<City> cityList = new ArrayList<City>();
            City city;
            city = new City();
            city.setId(1);
            city.setName("太原市");
            city.setProvinceId(1);
            cityList.add(city);

            city= new City();
            city.setId(2);
            city.setName("大同市");
            city.setProvinceId(1);
            cityList.add(city);
           
            city= new City();
            city.setId(3);
            city.setName("長治市");
            city.setProvinceId(1);
            cityList.add(city);

            cityMap.put(1, cityList);

            cityList = new ArrayList<City>();
            city= new City();
            city.setId(4);
            city.setName("西安市");
            city.setProvinceId(2);
            cityList.add(city);

            city= new City();
            city.setId(5);
            city.setName("漢中市");
            city.setProvinceId(2);
            cityList.add(city);
           
            city= new City();
            city.setId(6);
            city.setName("鹹陽市");
            city.setProvinceId(2);
            cityList.add(city);

            cityMap.put(2, cityList);
           
            cityList = new ArrayList<City>();
            city= new City();
            city.setId(7);
            city.setName("鄭州市");
            city.setProvinceId(3);
            cityList.add(city);

            city= new City();
            city.setId(8);
            city.setName("開封市");
            city.setProvinceId(3);
            cityList.add(city);
           
            city= new City();
            city.setId(8);
            city.setName("洛陽市");
            city.setProvinceId(3);
            cityList.add(city);

            cityMap.put(3, cityList);

            return SUCCESS;

 }
}

現在寫設定檔:

<action name="doubleSelect" class="com.student.action.DoubleSelectAction">
           <result name="success">/tagsDoubleSelect.jsp</result>
</action>

 

最後寫顯示級聯下拉式功能表的web頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>struts2級聯下拉式功能表樣本</title>
 </head>
 <body>

  <s:form name="f1">
   <s:doubleselect list="provinceList" listKey="id" listValue="name"
    doubleName="cityId"
    doubleList="cityMap.get(top.id)" doubleListKey="id" doubleListValue="name"/>
  </s:form>
 </body>
</html>

 

此處要注意的是top的用法,top代表的就是list當前選中的對象, 所以top.id對應的就是當前選中的Province對象中的ID, cityMap.get(top.id)即根據當前選中的Province對象中的ID來取出第二級下拉框的資料集合。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.