Struts2學習筆記之s:select標簽


貌似Struts2 標簽都快忘光了  今天先來一發struts2的select標簽:

首先從簡到難,靜態賦值:

1
< s : select name = "user.sex" list = "#{'0':'男','1':'女'}" label = "性別" headerKey = "" headerValue = "請選擇性別" > < / s : select >

這樣就會出現下拉框,默認是請選擇性別,點開后是男女,根據firebug,看到男女前面對應的是0和1,比起原生態的select標簽簡單多了。

注釋:這里從頁面來靜態構造下拉選項,關鍵點是list=”#{‘0′:’男’,’1′:’女’}” 通過它實現的select的兩個option 。headerKey指定默認的選項”key”,headerValue指定默認的選項”value”。

再看看設置下拉列表的數據:

1
2
3
4
5
6
7
8
     <%   
             java . util . HashMap map = new java . util . LinkedHashMap ( ) ;   
             map . put ( 1 , "A公司" ) ;   
             map . put ( 2 , "B公司" ) ;   
             map . put ( 3 , "C公司" ) ;   
             request . setAttribute ( "map" , map ) ;   
             request . setAttribute ( "aa" , "D公司" ) ;   
     %>

1
< s : select list = "#request.map"    label = "abc" listKey = "key" listValue = "value" value = "#request.aa"    headerKey = "0" headerValue = "aabb" > < / s : select >

value的值設置為了2,所以初始化下拉框時,會自動選擇key值為2的選項。

最后,配合Struts2的action看看效果吧:

UserDao的代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Repository ( "userDao" )
public interface UserDao extends SqlMapper {
 
     public List <User> selectAll ( ) ;
     public User getById ( Integer id ) ;
     public User loginCheck ( @Param ( "loginName" ) String login_name , @Param ( "password" ) String password ) ;
     public int updateUser ( User user ) ;
     public int deleteById ( Integer id ) ;
     public int add ( User user ) ;
     @Select ( "SELECT a.*,b.* from users a ,orgs b where a.org_id = b.id" )
     public List <Map> getUserInfo ( ) ;
     @Select ( "SELECT * from orgs ORDER BY id" )
     public List <Map> getOrgs ( ) ;
}

UserAction的代碼:

1
2
3
4
5
6
7
8
9
10
11
12
@Action ( "getuserinfo" )
public String getAllUsers ( ) {
List <Map> userList = userDao . getUserInfo ( ) ;
List <Map> orgs = userDao . getOrgs ( ) ;
if ( userList != null ) {
ServletActionContext . getRequest ( ) . setAttribute ( "userlist" , userList ) ;
ServletActionContext . getRequest ( ) . setAttribute ( "orgs" , orgs ) ;
return SUCCESS ;
} else {
return ERROR ;
}
}

試圖jsp頁面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
< div class = "adduserbox" >
     < div class = "tit" >新建帳號 <span> X < / span > < / div >
     < form action = "adduserinfo.feinno" method = "post" name = "saveForm" id = "saveForm" >
     < div class = "content1" >
     < div class = "name" >
     < div class = "fields" >姓名: < / div > < input type = "text" name = "name" placeholder = "請輸入姓名" autocomplete = "off" >
     < / div >
     < div class = "name" >
     < div class = "fields" >電話: < / div > < input type = "text" name = "svc_url"    placeholder = "請輸入電話號碼" autocomplete = "off" >
     < / div >
     < div class = "name" >
     < div class = "fields" >公司: < / div > < s : select name = "company" list = "#request.orgs" headerKey = "" headerValue = "請選擇公司" listKey = "id" listValue = "org_name" > < / s : select >
     < / div >
     < div class = "name" >
     < div class = "fields" >郵箱: < / div > < input type = "text" name = "img_url"    placeholder = "請輸入郵箱地址" autocomplete = "off" >
     < / div >
     < div class = "name" >
     < div class = "fields" >登錄帳號: < / div > < input type = "text" name = "img_url"    placeholder = "請輸入登陸賬號" autocomplete = "off" >
     < / div >
     < div class = "other" >
     < button class = "btn  left blue" id = "saveQuickService" >保存 < / button >
     < button class = " write btn  right" id = "calcle" >取消 < / button >
     < / div >
     < / div >
     < / form >
< / div >

這段代碼是jsp里面的一塊,點擊新增用戶彈出的彈出窗。

大家可以看到在“公司”這一個表單里的使用方法,首先給一個名稱name屬性,然后需要現實的List,list屬性,listKey說明下拉框的key值對應id(公司ID)字段,listValue說明下拉框的顯示部分對應org_name(公司名稱)字段。很簡單,很強大,很快捷。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM