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

818

被折叠的 条评论
为什么被折叠?



