在action封装数组,将其传到jsp页面,以checkbox形式显示

来源:互联网 发布:淘宝上的图片怎么做的 编辑:程序博客网 时间:2024/05/22 10:48
  HTML code<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
     String path = request.getContextPath();
     String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!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="myarr">
                for(var i=0;i<checkBoxs.length;i++){
                    if(checkBoxs[i].value=='${myarr}'){
                        checkBoxs[i].checked=true;
                    }
                }
              </c:forEach>
        }
        window.onload=function(){
            test();
        }
    </script>
   
  </head>
 
  <body>
     
        jsp页面中有几个checkbox
        <input type="checkbox" name="cb1" value="a">a
        <input type="checkbox" name="cb1" value="b">b
        <input type="checkbox" name="cb1" value="c">c
        <input type="checkbox" name="cb1" value="d">d
        <br>
        <!--
            <c:forEach items="${arr}" var="db">
                <input type="checkbox" name="cb1" value="db" <c:if test='${fn:contains(arr,db)}'>checked="true" </c:if>/>${db} <br />
            </c:forEach>
         -->
  </body>
</html>
后台:
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");
}

原创粉丝点击