Custom rendering in JSP based on class type without instance

How would you correctly list objects in jsp with different types? Say, for example, I have to display these different objects in that order.

One way could be to use a generic type or instance variable, but that means having a large switch / if statement to manage them:

<c:forEach var="o" items="${bigListofObjects}"  >
    <c:choose>
        <c:when test='${o.type=="simple"}' >
        <!-- render simple -->
        </c:when>
        <c:when test='${o.type=="complex"}' >
        <!-- render complex -->
        </c:when>
        <!-- etc etc ... -->
    </c:choose>
</c:forEach>

I could add a render () method for each class, but then that means mixing the view with the rest of the code.

What happens if I want to make another type later? Is there something I can do with custom jsp tags?

+5
source share
3 answers

jsp, . :

<c:forEach var="o" items="${bigListofObjects}"  >
    <c:import url="render-${o.type}.jsp"/>
</c:forEach>
+6

, HTML/JSP, . , . , , .

, .

. , , , CSS skinning .

+1

forEach, :

<c:forEach var="t" items="${listOfTypes}">
    <c:forEach var="o" items="${bigListofObjects}">
        <if test='${o.type==t}'>
            <!-- render -->
        </c:if>
    </c:forEach>
</c:forEach>
0

All Articles