最近做毕业设计用到Struts2 的标签库,遇到一些比较复杂的数据显示,个人还是比较喜欢用tag显示的,Struts2 tags内容丰富,但是所提供的文档不是很详细(个人认为)在showcase下的例子如:<s:select /> <s:doubleselect /> <s:updownselect /> <s:optiontransferselect />等都是一些简单的值显示,在实际的开发中并没有那么简单,如果我们要迭代显示List、Map、Set里的值,我们该怎样做呢?
看看html里的例子,
<select name="sex"> <option value="man">男</option> <option value="women">女</option> </select> |
Sex表示提交的name,man/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()%> </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显示如下:
<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"> <img height="415" alt="" src="http://www.blogjava.net/images/blogjava_net/bulktree/clip_image002.jpg" width="553" border="0"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype>
上面的代码都是一些简单的值显示,实际的开发所出现的数据都不是现成的。大家可能注意了这段代码:
<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; public<span styl 发表评论
最新评论
|
评论