Best practice in JSTL with multi-dimensional arrays
843836Apr 21 2005 — edited Apr 21 2005Hi,
I'm working in a project and I'm trying to convert some code into JSTL way, I have a first.jsp that calls addField(String, String) from fieldControl.jsp:
(first.jsp)
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ include file="fieldControl.jsp" %>
<html>
<head><title>JSP Page</title></head>
<body>
<%! String[][] list1;
%>
<%
list1 = new String[2][2];
list1[0][0]="first_1";
list1[0][1]="first_2";
list1[1][0]="second_1";
list1[1][1]="second_2";
for (int i=0;i<list1.length;i++){
String html;
html = addField (list1[0], list1[i][1]);
out.println(html);
}
%>
</body>
</html>
(fieldControl.jsp)
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page language="java" %>
<%! public void addField(String name,String label)
{
...
return ...
}
Now for JSTL I've this example from "JSTL pratical guide for JSP programmers" Sue Spielman:
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>
Display Results
</title>
</head>
<body>
<%-- Create a HashMap so we can add some values to it --%>
<jsp:useBean id="hash" class="java.util.HashMap" />
<%-- Add some values, so that we can loop over them --%>
<%
hash.put("apples","pie");
hash.put("oranges","juice");
hash.put("plums","pudding");
hash.put("peaches","jam");
%>
<br>
<c:forEach var="item" items="${hash}">
I like to use <c:out value="${item.key}" /> to make <c:out value="${hash[item.key]}" />
<br>
<br>
</c:forEach>
</body>
</html>
and my problem is :
1st - how to use the multi-dimensional array in this way (<jsp:useBean id="hash" class="java.util.HashMap" />) ? because if I use it like this
<% String[][] list1;
list1 = new String[2][2];
list1[0][0]="first_1";
list1[0][1]="first_1";
list1[1][0]="second_1";
list1[1][1]="second_2";%>
<c:out value="${list1}" />
<c:out value="${list1.lenght}" />
I get nothing,
also tryed <c:set var="test" value="<%=list1.length%>" /> and got "According to TLD or attribute directive in tag file, attribute value does not accept any expressions"
2nd hot to make the call to the method addField?
Thanks for any help, I really want to make this project using JSTL, PV