`
阅读更多
 

最近做毕业设计用到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显示如下:

<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

分享到:
评论

相关推荐

    j2ee-struts2-Select_DoubleSelect.rar_struts2 doubleselect

    J2EE中使用struts2实现的select和doubleselect标签

    struts2地市、县区二级联动下拉菜单 doubleselect标签

    用struts2 doubleselect标签实现的二级联动下拉菜单

    Struts2-Double-Select-Example

    Struts2-Double-Select-Example

    struts2 标签库 帮助文档

    Struts 2 标签库(文档手册) Tags-API-CLSW-JSP &lt;%@ taglib prefix="s" uri="/struts-tags" %&gt; 就能使用struts2.0的标签库 下面就介绍每个标签的具体应用实例说明:按字母排列 A: 1. 2. &lt;s:a href=""&gt;&lt;/s:a&gt;-...

    Struts2+API+标签全解+网页说明

    doubleselect标签:生成一个相互关联的列表框,该标签由两个下拉选择框组成。 datetimepicker标签:生成一个日期、时间下拉列表框。 head标签:生成HTML页面的HEAD部分。 file标签:在页面上生成一个上传文件元素...

    深入浅出Struts2(附源码)

    作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,如数据类型转换、文件上传和下载、提高Struts 2应用的安全性、调试与性能分析、FreeMarker、Velocity、Ajax,等等。跟随作者一道深入Struts 2,聆听...

    JSP_struts2标签大全

    JSP_struts2标签大全 1.a 3 2.action 3 3. actionerror 4 4. actionmessage 5 5. append 5 6. bean 7 7.checkbox 7 8.checkboxlist 8 9. combobox 9 10. conponent 9 11. date 11 12. datetimepicker 12 13. debug ...

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    书中介绍了如何利用Struts 2 来解决Web 应用开发中的常见问题,同时还深入浅出地探讨了许多能帮助程序员编写Struts 2 应用程序的技巧,如管理页面导航活动、输入验证、国际化和本地化、对Ajax 的支持,等等。书中...

    低清版 大型门户网站是这样炼成的.pdf

    2.3.4 struts 2中集成displaytag 83 2.4 struts 2国际化实现 85 2.4.1 web应用的中文本地化 85 2.4.2 struts 2应用的国际化 87 2.4.3 struts 2国际化语言的动态切换 89 2.5 struts 2的校验框架 90 2.5.1 在...

Global site tag (gtag.js) - Google Analytics