struts中使用多选的 html:select

本文详细介绍了在Struts框架中如何实现多选功能,包括在JSP页面中配置多选下拉框、在ActionForm中定义正确属性以及在Action中处理多选参数的步骤,并提供了示例代码。
 
在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");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值