struts2的select標籤用法


假如我們要在JSP中讓用戶選擇他屬於哪個國家

action裏有個User的bean對象,user這個PO裏有id,userName和Country對象,Country這個PO裏有id,countryName;
<s:select>靜態加載:
<s:select list="#{'0':'中國','1':'美國','2':'英國'}" cssStyle="width:130px;" name="user.country.id"></s:select>
<s:select>動態加載
方法1:
    後臺DAO取出對應的List<Country> countryList;
    後臺業務層將countryList轉化爲Map<Integer,String> countryMap,key爲id,value爲countryName;
    action中添加Map<Integer,String> countryMap對象並將業務層的MAP賦值給他;
    JSP代碼:   
        <s:select list="countryMap" name="user.country.id"></s:select>
        標籤中list屬性對應action中的countryMap,<option>的value值和顯示值對應Map的K/V對
方法2:後臺DAO取出對應的List<Country> countryList;
    action中添加List<Country> countryList對象並將DAO層的LIST賦值給他;
    JSP代碼:
        <s:select list="countryList" listKey="country.id" listValue="country.countryName" name="user.country.id">
        標籤中list屬性對應action中的countryList,<option>的value值和顯示值對應標籤中的listKey屬性(對應List封裝的PO即Country的id)和listValue屬性(對應List封裝的PO即Country 的countryName).
注:<s:select>的headerKey,headerValue爲默認的<option>的value值和顯示值
    例如:在action中添加一個Country country對象作爲JSP顯示的默認值
    JSP代碼:
    <s:select list="countryList" listKey="country.id" listValue="country.countryName" name="user.country.id" headerKey="country.id" headerValue="country.countryName">

發佈了41 篇原創文章 · 獲贊 35 · 訪問量 56萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章