tags:

views:

1213

answers:

5

I wanted to pass a List type object which is in the main JPS to an include JSP ( jsp:include). Since the parm only support strings, I cannot use the parm tag to pass List type data to the include file.

Uses examples:

<jsp:include page="/jsp/appList.jsp">
     <jsp:param name="applications" value="${applications}"/>
</jsp:include>

Or:

<jsp:include page="/jsp/appList.jsp">
     <jsp:param name="applications" value="${confirmed_applications}"/>
</jsp:include>
<jsp:include page="/jsp/appList.jsp">
    <jsp:param name="applications" value="${unconfirmed_applications}"/>
</jsp:include>
<jsp:include page="/jsp/appList.jsp">
    <jsp:param name="applications" value="${canceled_applications}"/>
</jsp:include>

I could create a Simple Tag Handler but I would like to know if there is an easier way.

+1  A: 

This is indeed what jsp tags are meant for. You create a .tag file that accepts attributes (not parameters), which can be of arbitrary type.

See this article for a good tutorial.

"Params" are analogous to the parameters you see in an HTTP query (the part of the URL following the '?').

Jonathan Feinberg
+1  A: 

How are you obtaining that list in your main JSP?

If you're getting it from the model (direct / indirect request / session attribute) you can pass the name of the attribute to your included JSP and re-obtain it from model there:

<jsp:include page="/jsp/appList.jsp">
  <jsp:param name="applications" value="confirmedApplications"/>
</jsp:include>

If you're generating or modifying that list in the JSP itself you've really got bigger problems then this :-) but you can bind your list to request under some attribute name and then use the above approach.

ChssPly76
A: 

Either you can use request or session objects for transferring objects.

Advantage

Request - Use request as its lifetime is lesser compared to a session object.

Session - You can retrieve the same object sometime later in another page or action

muthu
A: 

Assuming that you put the list in request scope and using applications as the name of your parameter you can refer to the list like this ${requestScope[param.applications]}

svachon
A: 

Agree with ChssPly76 that its probably a sign of trouble if the List is being generated in the JSP, but another alternate approach to get the object to the other JSP is to add it to the HttpServletRequest objects attribute field using a scriplet:

JSP with the List:

<%
request.setAttribute("theList", ListObject);
%>

The other JSP:

<%
List myList = (List) request.getAttribute("theList");
%>
MontyBongo