Struts下html:select可以多选时的处理方法

html:select是支持多选的,单选的时候很简单,后台就是接受一个String,多选的时候如何处理呢? 

1、创建一个可以多选的select控件需要在属性列表中加入multiple="true"这样的代码,如下: 
CODE:  SELECT ALL
        <html:select property="userlist" size="6" styleClass="uglimitselect" multiple="true">
          <%
             for (i=0; i<userlist.size(); i++) {
               tmpstring = (String)userlist.get(i);
          %>
          <html:option value="<%=tmpstring%>"><%=tmpstring%></html:option>
          <%
             }
          %>
        </html:select>


2. 定义成multiple之后,对应的ActionFormBean中field要定义成一个String数组,注意,不能用ArrayList或是其他东西,只能用String数组,如下:

CODE:  SELECT ALL
       <!-- Create UG Queue Limit Action -->
       <action   path="/CreateUGQueueLimit"
                 type="com.jointforce.action.CreateUGQueueLimitAction"
                 name="CreateUGQueueLimitForm"
                 scope="request"
                 input="createugqueuelimit">
           <forward name="moduleunavailable" path="/jsp/joblimitunavailable.jsp"/>
           <forward name="createugqueuelimit" path="/jsp/createugqueuelimit.jsp"/>
           <forward name="success" path="/jsp/createugqueuelimitresult.jsp"/>
       </action>

      <!-- Create UG Queue Limit -->
      <form-bean name="CreateUGQueueLimitForm"
                 type="org.apache.struts.validator.DynaValidatorForm">
          <form-property name="userlist" type="java.lang.String[]"/>
          <form-property name="grouplist" type="java.lang.String[]"/>
          <form-property name="hostlist" type="java.lang.String[]"/>
      </form-bean>


validation.xml中照旧:

CODE:  SELECT ALL
        <form name="CreateUGQueueLimitForm">
            <field property="userlist"></field>
            <field property="hostlist"></field>
            <field property="grouplist"></field>
        </form>

 

 最后在Action类里面这样就可以取到了: 


String[] userlist = (String[])PropertyUtils.getProperty(form, "userlist"); 

用一个for循环就可以取出东东: 

for (int i=0; i<userlist.length; i++) { 
System.out.println(userlist[i]); 
}

转载于:https://www.cnblogs.com/super119/archive/2011/01/13/1935008.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值