Struts如何实现动态的checkbox,希望高手们都来关注一下!200分 通常我都用以前旧有的方式写!struts里好多标签使用起来都不方便或许是我水平有限 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 其实方法一样了;通过ACTIONFORM把参数传过来;让它动态显示就行了; 希望大家关注,我想用struts的人,人人都会遇到这样的问题,如果能有个最终解决方法.我想这是一大贡献.敬请高手们关注!!! HITZXL(编程要厚道) 说的对。 checkbox的name应该是一样的<INPUT TYPE="checkbox" NAME="name" value="0"><INPUT TYPE="checkbox" NAME="name" value="1"><INPUT TYPE="checkbox" NAME="name" value="2">在ActionForm中,用int[] name(当然也可能是String[]、long[]),和相应的get、set方法,这样就能取到所有选中的值,然后遍历处理。对于Struts的checkbox标签,确实有局限,我已经重写了checkbox的标记库,以适应自己的需要。 laoer(laoer),老兄能给个例子,比如你自己该项的tag看看吗?我需要的是动态的. JSP:<html:checkbox property="check" value="1"/><html:checkbox property="check" value="2"/><html:checkbox property="check" value="3"/>Action Form(Dynamic Form):<form-property name="check" type="java.lang.String[]"/>Session bean:String[] x = (String[])map.get("check");是一个数组 view.jsp<%@ page contentType="text/html; charset=GBK" %><%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %><html:html><head><title>view Info</title></head><body><p align="center">查看疑问信息</p><table width="600" border="1" align="center" cellpadding="2" cellspacing="0"> <tr align="center"> <td colspan="2"><bean:write name="row" property="title"/></td> </tr> <tr> <td>网友:<bean:write name="row" property="nickname"/></td> <td>添加时间:<bean:write name="row" property="addtime"/></td> </tr> <tr> <td>问题类型: <html:radio name="row" property="questiontype" value="1"><bean:message key="add.label.questiontype.option1"/></html:radio> <html:radio name="row" property="questiontype" value="2"><bean:message key="add.label.questiontype.option2"/></html:radio> <html:radio name="row" property="questiontype" value="3"><bean:message key="add.label.questiontype.option3"/></html:radio> </td> <td>回复类别: <html:multibox name="row" property="replytype" value="1"/><bean:message key="add.label.replytype.option1"/> <html:multibox name="row" property="replytype" value="2"/><bean:message key="add.label.replytype.option2"/> </td> </tr> <tr> <td width="88" valign="top"><bean:message key="add.label.city"/></td> <td width="506"> <html:select name="row" property="city"> <html:option value="葫芦岛">葫芦岛1</html:option> <html:option value="沈阳">沈阳1</html:option> <html:option value="抚顺">抚顺1</html:option> </html:select> </td> </tr> <logic:notEmpty name="row" property="attach" > <tr> <td colspan="2"><bean:write name="row" property="attach" filter="false"/></td> </tr> </logic:notEmpty> <tr> <td colspan="2"><bean:write name="row" property="question"/></td> </tr></table></body></html:html> ViewAction.javapackage question;import org.apache.struts.action.*;import javax.servlet.http.*;import java.util.*;import java.sql.*;import org.apache.struts.taglib.html.BaseHandlerTag;public class ViewAction extends Action { public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { ResultSet rs = null; DB db = null; HashMap row = null; String sql = ""; String tmp = ""; int id ; try { id = Integer.parseInt(httpServletRequest.getParameter("id")); } catch(Exception e){ id = 0; } sql = "select title,nickname,addtime,replytype,questiontype,tele,email,attach,question,city from question where id =" + id; db = new DB(); db.getConnect(); try { rs = db.query(sql); row = new HashMap(1); if (rs.next()) { row.put("title", rs.getString("title")); row.put("nickname", rs.getString("nickname")); row.put("tele", rs.getString("tele")); row.put("email", rs.getString("email")); row.put("questiontype", rs.getString("questiontype")); row.put("replytype", rs.getString("replytype").split(",")); row.put("addtime", rs.getString("addtime")); row.put("question", rs.getString("question")); row.put("city", rs.getString("city")); if (rs.getString("attach").equals("")) { row.put("attach",""); } else { row.put("attach", "<a href='./upload/" + rs.getString("attach") + "' target='_blank'>View Attach</a>"); } } } catch(Exception e) { System.out.println("Fetch Info errors:" + e); } finally{ try{ rs.close(); db.close(); } catch(Exception e) { System.out.println("Close Connection Error:" + e); } } httpServletRequest.setAttribute("row", row); return actionMapping.findForward("view"); }} Struts中处理动态checkbox应该用multibox框 like this: <html:multibox property="ids" disabled="true"> <bean:write name="document" property="documentId"/> </html:multibox>然后你的bean属性应该是数组形式 like thisInteger ids[];{set ... get...} 我觉得这种东西在很多页面都会存在,应该做成通用控件,而不是在每个页面都写类似的代码.可以考虑用tiles作,配合ControlClass提供数据.带当前值作为参数进去.应该很方便. 根据上面的想法,做了一下,还可以. checkbox列表控件:1. 定义Tile模板. tiles-defs.xml: <definition controllerClass="mytest.MasterDataControler" name="list" path="/control/checkboxlist.jsp"/>2. 定义MasterDataControler.java:package mytest;import org.apache.struts.action.*;import javax.servlet.http.*;import org.apache.struts.tiles.ComponentContext;import javax.servlet.ServletContext;public class MasterDataControler implements org.apache.struts.tiles.Controller{ public void perform(ComponentContext tileContext, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) { request.setAttribute("master",new MasterDataFormBean()); //构造缓存主数据,或者根据用户参数动态构造. }}3. MasterDataFormBean.java:public class MasterDataFormBean { public String[] getList(){ String[] list = new String[]{"a","b","c"}; return list; }}4. checkboxlist.jsp:<tiles:useAttribute name="paramName"/><logic:iterate id="item" name="master" property="list"> <html:multibox property='<%=(String)paramName%>'><bean:write name="item"/></html:multibox> <bean:write name="item"/></logic:iterate>5. 在网页中调用控件: <tiles:insert definition="list" flush="false"> <tiles:put name="paramName" value="list"/> 这里每个jsp页面控件的名称可能会不同. </tiles:insert> 参考:http://husted.com/struts/tips/007.html <html:select property="assessyear" style="height:100;width:150" > <html:options collection="assessyear" property="value" labelProperty="label" /> </html:select> <html:select property="AAA"> <html:options collection="BBB" property="code" labelProperty="label" /> </html:select> 可参考http://dev.csdn.net/user/kui: 《Struts复选框的实现》 《Struts单选框的实现》 《Struts下拉框的实现》 求助!!! ext+ssh2实现树形结构显示 求Struts1.3登录实例 JSF 命令标签<h:commandButton type="button" 疑问 奇怪的Exception hibernate关联的问题? jsp 中如何屏蔽状态栏的超链地址 高分请教一个hibernate3调用存储过程的问题!急! 如何把struts源码加入工程,利用源码进行追踪调试? java里有什么规范 jsp里有什么规范 关于struts一问 怎样实现从服务器向所有与它连接的客户端发通知? 导数据--高分求教
<INPUT TYPE="checkbox" NAME="name" value="0">
<INPUT TYPE="checkbox" NAME="name" value="1">
<INPUT TYPE="checkbox" NAME="name" value="2">
在ActionForm中,用int[] name(当然也可能是String[]、long[]),和相应的get、set方法,这样就能取到所有选中的值,然后遍历处理。
对于Struts的checkbox标签,确实有局限,我已经重写了checkbox的标记库,以适应自己的需要。
<html:checkbox property="check" value="1"/>
<html:checkbox property="check" value="2"/>
<html:checkbox property="check" value="3"/>Action Form(Dynamic Form):
<form-property name="check" type="java.lang.String[]"/>Session bean:
String[] x = (String[])map.get("check");是一个数组
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<head>
<title>view Info</title>
</head>
<body>
<p align="center">查看疑问信息</p>
<table width="600" border="1" align="center" cellpadding="2" cellspacing="0">
<tr align="center">
<td colspan="2"><bean:write name="row" property="title"/></td>
</tr>
<tr>
<td>网友:<bean:write name="row" property="nickname"/></td>
<td>添加时间:<bean:write name="row" property="addtime"/></td>
</tr>
<tr>
<td>问题类型:
<html:radio name="row" property="questiontype" value="1"><bean:message key="add.label.questiontype.option1"/></html:radio>
<html:radio name="row" property="questiontype" value="2"><bean:message key="add.label.questiontype.option2"/></html:radio>
<html:radio name="row" property="questiontype" value="3"><bean:message key="add.label.questiontype.option3"/></html:radio>
</td>
<td>回复类别:
<html:multibox name="row" property="replytype" value="1"/><bean:message key="add.label.replytype.option1"/>
<html:multibox name="row" property="replytype" value="2"/><bean:message key="add.label.replytype.option2"/>
</td>
</tr>
<tr>
<td width="88" valign="top"><bean:message key="add.label.city"/></td>
<td width="506">
<html:select name="row" property="city">
<html:option value="葫芦岛">葫芦岛1</html:option>
<html:option value="沈阳">沈阳1</html:option>
<html:option value="抚顺">抚顺1</html:option>
</html:select>
</td>
</tr>
<logic:notEmpty name="row" property="attach" >
<tr>
<td colspan="2"><bean:write name="row" property="attach" filter="false"/></td>
</tr>
</logic:notEmpty>
<tr>
<td colspan="2"><bean:write name="row" property="question"/></td>
</tr></table>
</body>
</html:html>
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
import org.apache.struts.taglib.html.BaseHandlerTag;public class ViewAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
ResultSet rs = null;
DB db = null;
HashMap row = null;
String sql = "";
String tmp = "";
int id ; try {
id = Integer.parseInt(httpServletRequest.getParameter("id"));
}
catch(Exception e){
id = 0;
}
sql = "select title,nickname,addtime,replytype,questiontype,tele,email,attach,question,city from question where id =" + id;
db = new DB();
db.getConnect();
try {
rs = db.query(sql);
row = new HashMap(1);
if (rs.next()) {
row.put("title", rs.getString("title"));
row.put("nickname", rs.getString("nickname"));
row.put("tele", rs.getString("tele"));
row.put("email", rs.getString("email"));
row.put("questiontype", rs.getString("questiontype"));
row.put("replytype", rs.getString("replytype").split(","));
row.put("addtime", rs.getString("addtime"));
row.put("question", rs.getString("question"));
row.put("city", rs.getString("city")); if (rs.getString("attach").equals("")) {
row.put("attach","");
}
else {
row.put("attach",
"<a href='./upload/" + rs.getString("attach") +
"' target='_blank'>View Attach</a>");
}
} }
catch(Exception e) {
System.out.println("Fetch Info errors:" + e);
}
finally{
try{
rs.close();
db.close();
}
catch(Exception e) {
System.out.println("Close Connection Error:" + e);
}
}
httpServletRequest.setAttribute("row", row);
return actionMapping.findForward("view"); }
}
<bean:write name="document" property="documentId"/>
</html:multibox>然后你的bean属性应该是数组形式 like thisInteger ids[];{set ... get...}
1. 定义Tile模板. tiles-defs.xml:
<definition controllerClass="mytest.MasterDataControler" name="list" path="/control/checkboxlist.jsp"/>
2. 定义MasterDataControler.java:
package mytest;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import org.apache.struts.tiles.ComponentContext;
import javax.servlet.ServletContext;
public class MasterDataControler implements org.apache.struts.tiles.Controller{
public void perform(ComponentContext tileContext, HttpServletRequest request,
HttpServletResponse response,
ServletContext servletContext) {
request.setAttribute("master",new MasterDataFormBean()); //构造缓存主数据,或者根据用户参数动态构造.
}
}
3. MasterDataFormBean.java:
public class MasterDataFormBean {
public String[] getList(){
String[] list = new String[]{"a","b","c"};
return list;
}
}
4. checkboxlist.jsp:
<tiles:useAttribute name="paramName"/>
<logic:iterate id="item" name="master" property="list">
<html:multibox property='<%=(String)paramName%>'><bean:write name="item"/></html:multibox>
<bean:write name="item"/>
</logic:iterate>5. 在网页中调用控件:
<tiles:insert definition="list" flush="false">
<tiles:put name="paramName" value="list"/> 这里每个jsp页面控件的名称可能会不同.
</tiles:insert>
<html:options collection="assessyear" property="value" labelProperty="label" />
</html:select>
<html:options collection="BBB" property="code" labelProperty="label" />
</html:select>
《Struts单选框的实现》
《Struts下拉框的实现》