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/p/1935008.html