原来只作.net的开发工作,最近工作有新项目需要用structs,遇到几个问题。
问题一:
需要根据数据库动态生成关于checkbox的列表。类似于.net中的checkboxlist。
问题二:
需要获得动态生成的checkboxlist中哪几项被选中。
问题三:
返回各个被选中项的text属性拼成的字符串,以逗号分开。现在我在jsp中这样写的,还望大家指点。
<logic:iterate id="itTableDataItem" name="CreateSQLForm">
<tr>
<td>
<bean:write name="CreateSQLForm" property="list_TableDataItemName" />
</td>
<td colSpan = "2">
作为返回条件
<INPUT type="checkbox" name = "ckAsCondition">
</td>
<td colSpan = "2">
作为查询条件
<INPUT type="checkbox" name = "ckAsResult">
</td>
</tr>
</logic:iterate>
问题一:
需要根据数据库动态生成关于checkbox的列表。类似于.net中的checkboxlist。
问题二:
需要获得动态生成的checkboxlist中哪几项被选中。
问题三:
返回各个被选中项的text属性拼成的字符串,以逗号分开。现在我在jsp中这样写的,还望大家指点。
<logic:iterate id="itTableDataItem" name="CreateSQLForm">
<tr>
<td>
<bean:write name="CreateSQLForm" property="list_TableDataItemName" />
</td>
<td colSpan = "2">
作为返回条件
<INPUT type="checkbox" name = "ckAsCondition">
</td>
<td colSpan = "2">
作为查询条件
<INPUT type="checkbox" name = "ckAsResult">
</td>
</tr>
</logic:iterate>
第二个问题,你可以在后台,用request.getParameterValues()得到选中的checkbox值
在FORMBEAN中按数组接收
private String[] raID = null;
public String[] getRaID() {
return raID;
} public void setRaID(String[] raID) {
this.raID = raID;
}
在ACTION可以得到他们的值
String[] id = null;
RegAuditingForm regAuditingForm = (RegAuditingForm) form;
id = regAuditingForm.getRaID();//接收FORMBEAN中传过来的数组List raid = Arrays.asList(id);//把字符串数组存入LIST
用html:checkbox生成对象数组;
在action中用request.getParameterValues()得到数组;
处理数组,生成字符串
用html:checkbox生成对象数组;
在FORMBEAN中按数组接收
private String[] raID = null;public String[] getRaID() {
return raID;
}public void setRaID(String[] raID) {
this.raID = raID;
}
在ACTION可以得到他们的值
String[] id = null;
RegAuditingForm regAuditingForm = (RegAuditingForm) form;id = regAuditingForm.getRaID();//接收FORMBEAN中传过来的数组List raid = Arrays.asList(id);//把字符串数组存入LIST
嘿嘿,直接复制楼上几位大大的啦。实在不想打哈。个人以为这样做比较好,其实FORMBEAN是很好用的东西啦!
希望能给你帮助
struts标签
<html:checkbox property="addItem">xxxx</html:checkbox>
相当于Html标签<imput type="checkbox" name="addItem" value="on">xxxx</input>
应用实例:
<logic:iterate id="subject" name="subjectList">
<html:multibox property="subjects" >
<bean:write name="subject" property= "id"/>
</html:multibox>
<bean:write name="subject" property= "name"/>
</logic:iterate>
作用:
把subjectList中subject的Id保存在subjects数组中(String型)
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;public class ShowListAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// 模拟数据库操作,将爱好处理成List的形式,保存进request传给JSP页面
// 在此实现第一问题第一步,生成列表的内容
ArrayList al = new ArrayList();
al.add(new LabelValueBean("打球", "playball"));
al.add(new LabelValueBean("游戏", "playgame"));
al.add(new LabelValueBean("看书", "playbook"));
request.setAttribute("checkbox_List", al);
return mapping.findForward("toIndexPage");
}
}---------------------------------------------------------------------
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<html>
<head>
<title>JSP for IndexForm form</title>
</head>
<body>
在此实现第一问题第二步,显示列表的内容
<html:form action="/index">
<logic:iterate id="thislist" name="checkbox_List">
<bean:define id="value" name="thislist" property="value" />
<html:multibox property="aihao">
<bean:write name="thislist" property="value" />
</html:multibox>
<bean:write name="thislist" property="label" />
<br />
</logic:iterate>
<html:submit />
<html:cancel />
</html:form>
</body>
</html>---------------------------------------------------------
package com.yourcompany.struts.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.form.IndexForm;public class IndexAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
IndexForm indexForm = (IndexForm) form;// TODO Auto-generated method
// stub
// 第二问题:取得哪几个checkbox被选中
String[] aihao_action = indexForm.getAihao();
System.out.println("您选择的爱好是:");
// 问题三:
// 返回各个被选中项的text属性拼成的字符串,以逗号分开。
// 第三个问题您的要求是text
// 其实value和text为相同的值,变通一下即可实现功能
for (int i = 0; i < aihao_action.length; i++)
System.out.println(aihao_action[i]);
return null;
}
}
-----------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
<data-sources />
<form-beans >
<form-bean name="indexForm" type="com.yourcompany.struts.form.IndexForm" /> </form-beans> <global-exceptions />
<global-forwards >
<forward name="toIndexPage" path="/form/index.jsp" /> </global-forwards> <action-mappings >
<action
attribute="indexForm"
input="/form/index.jsp"
name="indexForm"
path="/index"
scope="request"
type="com.yourcompany.struts.action.IndexAction" />
<action path="/showList" type="com.yourcompany.struts.action.ShowListAction" /> </action-mappings> <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>