This post describes some of my recent experiences in using JSP and JSTL. For many probably highly trivial.
A dynamic include of a JSP (fragment), using jsp:include, allows passing of parameters, using jsp:param. However, such parameters are always String values. I needed to pass an array (String[]) and could not figure out how to. Using a scriptlet to define an array, add it to the pageContext (see for example) and reference it in the JSP fragment did not work:
<% String[] lov = {"a","b", "c"}; pageContext.setAttribute("lov", lov); %>
This is only logical once you realize that a jsp:include turns over control to a JSP that to all intents a purposes is a new, stand-alone JSP with its own pageContext. However, JSTL comes to the rescue. It turns out that the forEach tag recognizes a comma separated list of values. Note however that JSTL does not now consider the parameter a String[], it is still a String.
Code to call the JSP fragment:
<jsp:include page="lovPopup.jsp" flush="true"> <jsp:param name="lovParam" value="a,b,c"/> </jsp:include>
Code in called JSP fragment:
<bean:parameter id="lov" name="lovParam"/> <c:forEach var="listItem" items="${lov}"/> <c: out value="${listItem}" /> </c> </c>
It is important that the JSP fragment is also “JSTL enabled” (initially I erroneously thought that somehow this JSP would inherit the taglibraries imported by the JSP that invokes it), by importing the Core JSTL library:
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
Now it is not possible to refer to
${lov [1]};
even though forEach knows how to iterate through a comma-separated String, it does not mean that it is equivalent to a String[] (error: Unable to find a value for “1” in object of class “java.lang.String” using operator “[]” (null)).
It should be possible to put an attribute of type String[] in the requestScope, rather than the pageScope, and thus transfer a typed object, rather than just a String, to the included JSP fragent.
<% String[] lov = {"a","b", "c"}; pageContext.setAttribute("lov", lov, PageContext.REQUEST_SCOPE); %>
and to get access to the lov object in the included JSP fragment:
<c:set var="list" value="${lov}" scope="request" /> <c: out value="${list [1]}" /> <c:forEach var="listItem" items="${list}"/> <c: out value="${listItem}" /> </c:forEach>
how to use the jstl core(url) for querystrings.i want to pass the url with query strings.
even i can not able to use this query string url for ‘set’ tag all so .plz give me possible solution.