var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-20738293-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script')"/>

          jutleo
          歡迎走進有風的地方~~
          posts - 63,  comments - 279,  trackbacks - 0
           

          最近做畢業設計用到Struts2 的標簽庫,遇到一些比較復雜的數據顯示,個人還是比較喜歡用tag顯示的,Struts2 tags內容豐富,但是所提供的文檔不是很詳細(個人認為)在showcase下的例子如:<s:select /> <s:doubleselect /> <s:updownselect /> <s:optiontransferselect />等都是一些簡單的值顯示,在實際的開發中并沒有那么簡單,如果我們要迭代顯示ListMapSet里的值,我們該怎樣做呢?

             
          看看html里的例子,

          <select name="sex">

              <option value="man"></option>

              <option value="women"></option>

          </select>

          Sex表示提交的nameman/women是對應頁面顯示提交后所代表的值,男/女則為頁面最終看到的值

             
          而如果我們要顯示一個List集合里的數據該怎么做呢?
             
          看下面的Jsp頁面:

          <select name="department">

              <%

                 Department department = null;

                 List list = (List) request.getAttribute("list");

                 Iterator iter = list.iterator();

                 while (iter.hasNext()) {

                     department = (Department) iter.next();

              %>

              <option value="<%=department.getDep_name() %>"><%=department.getDep_name()%>&nbsp;&nbsp;&nbsp;</option>

              <%

              }

              %>

          </select>

          迭代的是Department的屬性dep_name,這樣顯示顯得很麻煩,如果Iterator輸出可能會好點,采用JSTL輸出:

          <c:forEach var="department" items="" varStatus="status">

              <tr>

                 <td>${status.dep_name }</td>

                 <td>${status.dep_id }</td>

                 <td>......</td>

              </tr>

          </c:forEach>


          現在看看Struts2的例子:這是Strust2 showcase例子

          <%@ page contentType="text/html; charset=UTF-8"%>

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

          <html>

          <head>

          <title>Test</title>

          </head>

          <body>

          <center><br>

          <br>

          <br>

          <hr>

          <br>

          <br>

          <s:form action="test_showPost" method="post" theme="simple">

              <table>

                 <tr>

                     <td><s:select

                        list="{'Windows','Linux','Java','.net','Pertl','PHP'}"

                        name="program" tooltip="select your program" /></td>

                 </tr>

                  <tr>

                     <td><s:select list="posts" name="post.post_name"

                        listKey="post_name" listValue="post_name" headerKey="0"

                        headerValue="請選擇你的職位" required="true"></s:select></td>

                 </tr>

                 <tr>

                     <td><s:checkboxlist name="skills1" label="Skills 1"

                        tooltip="bulktree" list="{'Java', '.Net', 'RoR', 'PHP' }"

                        value="{'Java', '.Net' }" /></td>

                 </tr>

                 <tr>

                     <td><s:checkboxlist name="skills2" label="Skills 2"

                        tooltip="bulktree" list="#{1:'Java', 2:'.Net', 3:'RoR', 4:'PHP' }"

                        listKey="key" listValue="value" value="{1, 2, 3 }" /></td>

                 </tr>

                 <tr>

                     <td><s:doubleselect label="doubleselect test1" name="menu"

                        list="{'fruit','other'}" doubleName="dishes"

                        doubleList="top == 'fruit' ? {'apple', 'orange'} : {'monkey', 'chicken'}" />

                     </td>

                 </tr>

                 <tr>

                     <td><s:updownselect label="Favourite Countries"

                        list="#{'england':'England', 'america':'America', 'germany':'Germany'}"

                        name="prioritisedFavouriteCountries" headerKey="-1"

                        headerValue="--- Please Order Them Accordingly ---"

                        emptyOption="true" /></td>

                 </tr>

                 <tr>

                     <td><s:optiontransferselect

                        tooltip="Select Your Favourite Cartoon Characters"

                        label="Favourite Cartoons Characters"

                        name="leftSideCartoonCharacters" leftTitle="Left Title"

                        rightTitle="Right Title" list="{'Popeye', 'He-Man', 'Spiderman'}"

                        multiple="true" headerKey="headerKey"

                        headerValue="--- Please Select ---" emptyOption="true"

                        doubleList="{'Superman', 'Mickey Mouse', 'Donald Duck'}"

                        doubleName="rightSideCartoonCharacters"

                        doubleHeaderKey="doubleHeaderKey"

                        doubleHeaderValue="--- Please Select ---" doubleEmptyOption="true"

                        doubleMultiple="true" /></td>

                 </tr>

                 <tr>

                     <td><s:submit></s:submit></td>

                 </tr>

              </table>

          </s:form></center>

          </body>

          </html>

          注意:上面的代碼不需要用table布局,Struts2內置了表格功能,run顯示如下:

           


             
          上面的代碼都是一些簡單的值顯示,實際的開發所出現的數據都不是現成的。大家可能注意了這段代碼:
             

          <tr>

                     <td><s:select list="posts" name="post.post_name"

                        listKey="post_name" listValue="post_name" headerKey="0"

                        headerValue="請選擇你的職位" required="true"></s:select></td>

                 </tr>


          下來就來說說Struts2 tag怎么顯示List/Map/Set里的值:
          采用POJO方式訪問 VO是一些最基本的getter/setter省略不寫。
              action
          代碼:

          package com.bulktree.AutoOffice.action;

          import java.util.List;

          import java.util.Map;

          import com.bulktree.AutoOffice.factory.DAOFactory;

          import com.bulktree.AutoOffice.vo.Client;

          import com.bulktree.AutoOffice.vo.ClientUser;

          import com.bulktree.AutoOffice.vo.User;

          import com.opensymphony.xwork2.ActionContext;

          import com.opensymphony.xwork2.ActionSupport;

          publicclass ClientUserAction extends ActionSupport {

             

              private List<Client> clients;

              private List<User> users;

             

              public List<Client> getClients() {

                 returnclients;

              }

              publicvoid setClients(List<Client> clients) {

                 this.clients = clients;

              }

              public List<User> getUsers() {

                 returnusers;

              }

              publicvoid setUsers(List<User> users) {

                 this.users = users;

              }

              public String queryClientID() throws Exception {

                 Map session = ActionContext.getContext().getSession();

                 String userid = (String)session.get("userid");

                

                 setUsers(DAOFactory.getEmployeeInstance().queryUidUserid());

                 setClients(DAOFactory.getClientInstance().queryByAll(userid));

                

                 returnSUCCESS;

              }

          }


             
          下面是用來測試上面actionjsp頁面:分別使用了<s:select/> <s:doubleselect /> <s:updownselect />來接收List集合里的值

          <s:form action="clientuser_changeClient" method="post">

                 <s:doubleselect list="clients" name="client.client_id"

                     listKey="client_id" listValue="client_id"

                     doubleName="client.client_name" doubleList="clients"

                     doubleListKey="client_name" doubleListValue="client_name" />

                 <s:updownselect label="All Clients ID" tooltip="show all clients"

                     list="clients" headerKey="0" headerValue="--所有客戶編號--"

                     listKey="client_id" listValue="client_id" emptyOption="true"

                     moveUpLabel="向上" moveDownLabel="向下" selectAllLabel="全選" />

                 <s:updownselect label="All Clients name" tooltip="show all clients"

                     list="clients" headerKey="0" headerValue="--所有客戶姓名--"

                     listKey="client_name" listValue="client_name" moveUpLabel="向上"

                     moveDownLabel="向下" selectAllLabel="全選" emptyOption="true" />

                 <s:select list="clients" name="clientuser.client_id"

                     tooltip="Change Your Client" label="選擇你將要轉讓的客戶" listKey="client_id"

                     listValue="client_id" required="true" />

                 <s:select list="users" name="clientuser.userid" label="將要轉讓給同事"

                     tooltip="Choose your partner" listKey="userid" listValue="userid"

                     required="true" />

                 <s:submit value=" 確認轉讓 " onclick="alert('轉讓后你就失去了該客戶');" />

              </s:form>


             
          說說最簡單的<s:selelct />其他的以此類推:
          ·select標簽必須屬性只有一個為List
          ·select一定要有值,否則出錯。如果我們在html中使用select時會有個默認的值,在Struts2中也是一樣的,如果List,沒有值可以加上headerKey,headerValue就可以通過。
          ·List屬性的值在Action中定義,必須為一個迭代的List/Map/Set,本例采用List
          · listKey對應html表單select中的value,listValue對應html表單中的option
          ·List/SetlistKeylistValue是一樣的
          ·如果是Map,則mapkey對應key,mapvalue對應value
             
          如下代碼:

          <s:select list="clients" name="clientuser.client_id"

                     tooltip="Change Your Client" label="選擇你將要轉讓的客戶" listKey="client_id"

                     listValue="client_id" required="true" />

              Clientsactionlist的對象,也就是getter/setter方法的名字,Struts2支持POJO訪問,listKey的值”client_id”則為VO對象 (client)的屬性(client_id(Struts2支持OGNL)我們還可以加上headerKeyheaderValue用以顯示首行的提示,大家可以加上試試,注意:headerKey的值不能為-1否則編譯不能通過。
              <s:doubleselect />
          <s:select />運行機制是一樣的,不同的就是<s:doubleselect />顯示的是兩個list/doubleList的值,doubleList的值牽制于list的值,它的內部實現機制是采用JavaScript
             

          <s:doubleselect list="clients" name="client.client_id"

                     listKey="client_id" listValue="client_id"

                     doubleName="client.client_name" doubleList="clients"

                     doubleListKey="client_name" doubleListValue="client_name" />

              這個<s:doubleselect />是有問題的,只是為了演示有值,但是沒有真正起到doubleselect的作用,doubleList是按編號取值的,doubleList對應Map中一個keyvalue。采用本例的話可以把這樣做:

          Map<Integer, List<clients>> maps = new HashMap<Integer, List<clients>>();

          maps.put(1, clients);

          maps.put(2, clients);

          maps.put(3, clients);

           
          maps
          key為第一級下拉列表的listKeytopclient的實例

          <s:doubleselect list="clients" name="client.client_id"

                     listKey="id" listValue="client_id"

                     doubleName="client.client_name" doubleList="maps.get(top. id)"

                     doubleListKey="client_name" doubleListValue="client_name" />

          posted on 2008-04-02 08:46 凌晨風 閱讀(10415) 評論(8)  編輯  收藏 所屬分類: Spring/Hibernate/Struts2

          FeedBack:
          # re: Struts2中select/doubleselect標簽數據顯示
          2008-04-02 09:07 | 凌晨風
          順便說說,畢業在即,我的系統側重業務邏輯,主要是想用Struts2,嚴格按照MVC三大模塊做,業務邏輯全部封裝在DAO工廠中,前臺JSP顯示OGNL,由于導師要求后面加了QQ/MSN、短信平臺(測試成功未實現),哪位能提供個就業的機會,本人感激不盡,后續將繼續整理系統的相關知識發布上來。謝謝關注!  回復  更多評論
            
          # re: Struts2中select/doubleselect標簽數據顯示
          2008-05-08 17:31 | 愛愛愛
          建議業務邏輯放在Bo 中  回復  更多評論
            
          # re: Struts2中select/doubleselect標簽數據顯示
          2008-07-08 14:21 | 汽車
          寫的真好,struts2就是好用.  回復  更多評論
            
          # re: Struts2中select/doubleselect標簽數據顯示
          2008-08-14 17:35 | KingBack
          嗨, 你好, select List試了半天總是不成功, 能把你用select的完整代碼貼出來嗎?  回復  更多評論
            
          # re: Struts2中select/doubleselect標簽數據顯示
          2008-09-13 14:50 | waylon
          LZ能不能把源代碼貼出來啊,我調試了兩天,沒成功,有個錯誤
          tag 'select', field 'list', name 'post.post_name': The requested list key 'Category' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location  回復  更多評論
            
          # re: Struts2中select/doubleselect標簽數據顯示
          2008-11-19 19:02 | 有點暈
          select的還是不行,可不可以把你
          <tr>

          <td><s:select list="posts" name="post.post_name"

          listKey="post_name" listValue="post_name" headerKey="0"

          headerValue="請選擇你的職位" required="true"></s:select></td>

          </tr>
          對應的action代碼貼出來看看呢?  回復  更多評論
            
          # re: Struts2中select/doubleselect標簽數據顯示
          2008-11-20 10:20 | 凌晨風
          這些是我畢業設計里的東東摘出來寫了點,最近忙一項目沒有時間找這些,你要是需要的話我把整個包發給你吧!laoshulin@gmail.com  回復  更多評論
            
          # re: Struts2中select/doubleselect標簽數據顯示
          2009-01-03 18:27 | 曾紅偉
          其實作者沒有寫全,s:select要想從數據庫中獲取值,首先創建一個action,然后在s:select標簽前加一段話:
          <s:action name="***" id="***"></s:action>
          <s:set name="**" value="#***.**"></s:set>  回復  更多評論
            

          <2008年4月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          常用鏈接

          留言簿(11)

          我參與的團隊

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          新聞分類

          新聞檔案

          收藏夾

          圍脖

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 揭阳市| 犍为县| 普陀区| 页游| 松滋市| 绥德县| 旅游| 江华| 昆明市| 河源市| 连江县| 富川| 西峡县| 大石桥市| 克拉玛依市| 新闻| 石首市| 肥城市| 民和| 云阳县| 钦州市| 罗源县| 镇雄县| 宁夏| 晋江市| 清水县| 山西省| 平邑县| 青神县| 秭归县| 布拖县| 塘沽区| 榆社县| 冀州市| 阿尔山市| 宣化县| 工布江达县| 古交市| 温泉县| 上思县| 定兴县|