Search code examples
javajspjsp-tags

How do I display different content with a jsp:include and jsp:param


I have two versions of a component to be displayed based on the url string for example:

http://localhost/index.li?id=a

This is supposed to display version 1

http://localhost/index.li?id=b

This is supposed to display version 2

What I was told to do is the following in the parent jsp file:

<jsp:include page="/somepage/components/acomponent.jsp">
        <jsp:param name="param1" value="value1"/>
        <jsp:param name="param2" value="value2"/>
</jsp:include>

The question I have is in my acomponent.jsp what name and values do I use to switch between versions? For example:

<div id="version1"></div>

<div id="version2"></div>

Solution

  • You could get the id off of the URI (either with JSTL or Scriptlets) and then pass that into your acomponent.jsp file like so.

    <jsp:include page= "/somepage/components/acomponent.jsp">
        <jsp:param name="value1" value="<id you got off of the URI>"
    </jsp:include>
    

    Then, within your acomponent.jsp file, you could do something like this:

    <c:choose>
        <c:when test="${value1 eq 'a'}">
            --JSP CODE FOR WHEN A NEEDS TO BE USED
        </c:when>
        <c:otherwise>
            --JSP CODE FOR WHEN B NEEDS TO BE USED
        </c:otherwise>
    </c:choose>
    

    Hope this helps.