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

          jutleo
          歡迎走進(jìn)有風(fēng)的地方~~
          posts - 63,  comments - 279,  trackbacks - 0
           

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

             
          看看html里的例子,

          <select name="sex">

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

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

          </select>

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

             
          而如果我們要顯示一個(gè)List集合里的數(shù)據(jù)該怎么做呢?
             
          看下面的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輸出可能會好點(diǎn),采用JSTL輸出:

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

              <tr>

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

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

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

              </tr>

          </c:forEach>


          現(xiàn)在看看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內(nèi)置了表格功能,run顯示如下:

           


             
          上面的代碼都是一些簡單的值顯示,實(shí)際的開發(fā)所出現(xiàn)的數(shù)據(jù)都不是現(xiàn)成的。大家可能注意了這段代碼:
             

          <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="選擇你將要轉(zhuǎn)讓的客戶" listKey="client_id"

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

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

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

                     required="true" />

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

              </s:form>


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

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

                     tooltip="Change Your Client" label="選擇你將要轉(zhuǎn)讓的客戶" 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 />運(yùn)行機(jī)制是一樣的,不同的就是<s:doubleselect />顯示的是兩個(gè)list/doubleList的值,doubleList的值牽制于list的值,它的內(nèi)部實(shí)現(xiàn)機(jī)制是采用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" />

              這個(gè)<s:doubleselect />是有問題的,只是為了演示有值,但是沒有真正起到doubleselect的作用,doubleList是按編號取值的,doubleList對應(yīng)Map中一個(gè)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的實(shí)例

          <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 凌晨風(fēng) 閱讀(10424) 評論(8)  編輯  收藏 所屬分類: Spring/Hibernate/Struts2

          FeedBack:
          # re: Struts2中select/doubleselect標(biāo)簽數(shù)據(jù)顯示
          2008-04-02 09:07 | 凌晨風(fēng)
          順便說說,畢業(yè)在即,我的系統(tǒng)側(cè)重業(yè)務(wù)邏輯,主要是想用Struts2,嚴(yán)格按照MVC三大模塊做,業(yè)務(wù)邏輯全部封裝在DAO工廠中,前臺JSP顯示OGNL,由于導(dǎo)師要求后面加了QQ/MSN、短信平臺(測試成功未實(shí)現(xiàn)),哪位能提供個(gè)就業(yè)的機(jī)會,本人感激不盡,后續(xù)將繼續(xù)整理系統(tǒng)的相關(guān)知識發(fā)布上來。謝謝關(guān)注!  回復(fù)  更多評論
            
          # re: Struts2中select/doubleselect標(biāo)簽數(shù)據(jù)顯示
          2008-05-08 17:31 | 愛愛愛
          建議業(yè)務(wù)邏輯放在Bo 中  回復(fù)  更多評論
            
          # re: Struts2中select/doubleselect標(biāo)簽數(shù)據(jù)顯示
          2008-07-08 14:21 | 汽車
          寫的真好,struts2就是好用.  回復(fù)  更多評論
            
          # re: Struts2中select/doubleselect標(biāo)簽數(shù)據(jù)顯示
          2008-08-14 17:35 | KingBack
          嗨, 你好, select List試了半天總是不成功, 能把你用select的完整代碼貼出來嗎?  回復(fù)  更多評論
            
          # re: Struts2中select/doubleselect標(biāo)簽數(shù)據(jù)顯示
          2008-09-13 14:50 | waylon
          LZ能不能把源代碼貼出來啊,我調(diào)試了兩天,沒成功,有個(gè)錯(cuò)誤
          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  回復(fù)  更多評論
            
          # re: Struts2中select/doubleselect標(biāo)簽數(shù)據(jù)顯示
          2008-11-19 19:02 | 有點(diǎn)暈
          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>
          對應(yīng)的action代碼貼出來看看呢?  回復(fù)  更多評論
            
          # re: Struts2中select/doubleselect標(biāo)簽數(shù)據(jù)顯示
          2008-11-20 10:20 | 凌晨風(fēng)
          這些是我畢業(yè)設(shè)計(jì)里的東東摘出來寫了點(diǎn),最近忙一項(xiàng)目沒有時(shí)間找這些,你要是需要的話我把整個(gè)包發(fā)給你吧!laoshulin@gmail.com  回復(fù)  更多評論
            
          # re: Struts2中select/doubleselect標(biāo)簽數(shù)據(jù)顯示
          2009-01-03 18:27 | 曾紅偉
          其實(shí)作者沒有寫全,s:select要想從數(shù)據(jù)庫中獲取值,首先創(chuàng)建一個(gè)action,然后在s:select標(biāo)簽前加一段話:
          <s:action name="***" id="***"></s:action>
          <s:set name="**" value="#***.**"></s:set>  回復(fù)  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

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

          常用鏈接

          留言簿(11)

          我參與的團(tuán)隊(duì)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          新聞分類

          新聞檔案

          收藏夾

          圍脖

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 临城县| 玉龙| 无锡市| 叶城县| 昌乐县| 金溪县| 武宣县| 汕头市| 承德市| 虎林市| 嘉禾县| 达尔| 西乌珠穆沁旗| 镇雄县| 邯郸市| 漳浦县| 汨罗市| 巢湖市| 旌德县| 成武县| 江油市| 扶绥县| 扬州市| 淄博市| 安陆市| 长汀县| 唐山市| 正宁县| 始兴县| 大名县| 临颍县| 河西区| 阆中市| 盐池县| 荥阳市| 西林县| 乌鲁木齐县| 元谋县| 托克托县| 武穴市| 马关县|