J2EE社区

苟有恒,何必三更起五更眠;
最无益,只怕一日曝十日寒.
posts - 241, comments - 318, trackbacks - 0, articles - 16

Struts2中select/doubleselect标签数据显示

Posted on 2010-05-03 18:35 xcp 阅读(8244) 评论(1)  编辑  收藏 所属分类: struts2

最近做毕业设计用到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" />




名称: ♪4C.ESL | .↗Evon
口号: 遇到新问题♪先要寻找一个方案乄而不是创造一个方案こ
mail: 联系我


Feedback

# re: Struts2中select/doubleselect标签数据显示   回复  更多评论   

2013-12-11 19:43 by zhangsan' or 'a'='a' --
haoaho

只有注册用户登录后才能发表评论。


网站导航: