简体   繁体   中英

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>

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.

Can't you use <c:choose>? It has to be only jsp:include and jsp:param?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM