<%@ 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.οnlοad=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");
}