从action传到页面一个数组,然后用这个数组里的值控制页面里一组checkbox默认被选中
如action出过去 a , b , cpublic ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String[] arr = { "a", "b", "c" };
request.setAttribute("arr", arr);
return new ActionForward("/index.jsp");
}index.jsp页面中有几个checkbox
<input type="checkbox" name="cb1" value="a">a<br>
<input type="checkbox" name="cb1" value="b">b<br>
<input type="checkbox" name="cb1" value="c">c<br>
<input type="checkbox" name="cb1" value="d">d<br>
...我希望用Javascript方法,默认选中前三组,但我现在不知道怎么在jsp页面中接收action中传来的数组

解决方案 »

  1.   

    public ActionForward execute(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response) { 
    // TODO Auto-generated method stub 
    String[] arr = { "a", "b", "c" }; 
    request.setAttribute("arr", arr); String[] arrDb = { "a", "b", "c", "d" }; //注意元数据,例如从数据库取出来
    request.setAttribute("arrDb", arrDb);return new ActionForward("/index.jsp"); 
    } <c:forEach items="${arrDb}" var="db">
        <input type="checkbox" name="cb1" value="db" <c:if test='${fn:contains(arr,db)}'>checked="true" </c:if>/>${db} <br />
    </c:forEach>
      

  2.   

    少写了$:
    value="${db}" 
      

  3.   

    页面
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'demo.jsp' starting page</title>
    <script type="text/javascript">
    function test(){
    var checkBoxs = document.getElementsByName("cb1");
    <c:forEach items="${arr}" var="wangjian">
    for(var i=0;i<checkBoxs.length;i++){
    if(checkBoxs[i].value=='${wangjian}'){
    checkBoxs[i].checked=true;
    }
    }
       </c:forEach>
    }
    window.onload=function(){
    test();
    }
    </script>

      </head>
      
      <body>后台 public ActionForward demo(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    String[] arr = { "a", "b", "c" }; 
    request.setAttribute("arr", arr);
    return mapping.findForward("test");
    }实验了。 看下吧
      

  4.   

    <c:forEach items="${arr}" var="wangjian">这个标签想使用,在jsp页面中要引入什么东西?
      

  5.   

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
      

  6.   

    先把数组传到JSP中,然后在JAVASCRIPT中处理.在<body onLoad="">中加载处理的方法.不知道适用不?
      

  7.   

    兄弟你可以直接用struts框架就可以了!!!
      

  8.   

    input标签里嵌套一个if标签来控制input标签的checked属性有么有