Reputation: 5367
I have a JSP which composes a List of Objects, then renders JSP fragments depending on the Class of each of the objects in the List. At the moment, this is done with a huge chain of if statements inside the 'parent' JSP:
if( bean.getFilterChildByType( Level.class ) != null )
{
%> <jsp:include page="filters/level.jsp"/> <%
}
if( bean.getFilterChildByType( Sources.class ) != null )
{
%> <jsp:include page="filters/sources.jsp"/> <%
}
...
So, my question is, in JSP (Tomcat) is it possible to achieve this same functionality without an if chain, just by iterating the Objects in the list and perhaps taking advantage of the naming convention "Class name".jsp ? I've played with:
<%@ include file="filename" %>
but this doesn't seem to allow variables in the file-name either.
Upvotes: 2
Views: 502
Reputation: 1415
This is a tough one!
If the included jsp files (level.jsp, source.jsp, etc) are not too complex, what about shifting the HTML from those files over to a function call of the objects you are calling bean.getFilterChildByType(...) on?
That way, instead of a large if/else tree, you could then just call:
String html = bean.getHtmlForType();
...would likely work out much cleaner in a loop too.
Upvotes: 0
Reputation: 6831
Resolve the appropriate jsp to be included (based on bean.getFilterChildByType
) at the controller side and then just pass the name of the jsp to the container jsp. Now this can be easily included.
Upvotes: 0
Reputation: 6490
Something like this should work
<jsp:include page="filters/<%=filename%>.jsp"/>
Upvotes: 2