struts中使用多選的 html:select

來源:互聯網
上載者:User

 

在Struts中使用多選的select時,應該做如下設定:
1. jsp頁面 中要在select元素中加入multiple="true"選項;
2. ActionForm 中應將改參數設定成String[]類型;
3. Action 中應該遍曆String數組,進行所需的商務邏輯操作。

範例程式碼如下:
jsp -----------------------------------------------------
<%@ page language="java" import="java.util.*" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
  <head>
  </head>
  <body>
   <html:form action="/action1">
   <html:select property="param1" multiple="true">
       <html:option value="111">111hehe</html:option>
       <html:option value="222">222hehe</html:option>
       <html:option value="333">333hehe</html:option>
   </html:select>
   <html:submit value="sub"/>
   </html:form>
  </body>
</html>

ActionForm -------------------------------------------
public class Form1Form extends ActionForm {
    private String param1[];
    public String[] getParam1() {
        return param1;
    }
    public void setParam1(String[] param1) {
        this.param1 = param1;
    }
    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
        return null;
    }
    public void reset(ActionMapping mapping, HttpServletRequest request) {

    }
}

Action ------------------------------------------------
public class Action1Action extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
       
        Form1Form form1 = (Form1Form) form;
        String param1[] = form1.getParam1();
        for (int i = 0; i < param1.length; i++) {
            String param = param1[i];
            System.out.println(i + ":" + param);
        }
        return mapping.findForward("jsp2");
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.