0

I'm making a form to add events to a list but I need to be able to see in that form the previous events from that list. I have a jsp that shows a list of events but it takes an attribute usually added by the controller when you access the list directly from the browser.

So how can I add the list jsp filling that attribute so it shows a list and not just the headers?

I have alreay included the jsp using

<jsp:include page="comp_list.jsp"></jsp:include>

And it shows the headers but as there is no attribute it shows no list. I tried adding attributes to the include like:

<jsp:include page="comp_list.jsp">
  <jsp:attribute name="compensaciones">
    ${compensaciones}
  </jsp:attribute>
</jsp:include>

But when I do it it shows an error telling me that this cannot be done.

Also tried params but that would not be the answer for me because params are treated in the controller and not in the jsp itself.

I'm just getting the header of the list which is fine, but i would like to see the list.

Edit: this is how i build the list in case you are wondering

<tbody>
  <c:forEach var="comp" items="${compensaciones}">
    <td>${comp.getSomething()}</td>
    ...
  </c:forEach>
</tbody>
MREugeneJ7
  • 1
  • 2
  • 8
  • Did you tried this way https://stackoverflow.com/questions/9110148/include-another-jsp-file#answer-23631260? – soorapadman Mar 29 '19 at 11:13
  • Possible duplicate of [Passing parameters to another JSP file using tag](https://stackoverflow.com/questions/19150683/passing-parameters-to-another-jsp-file-using-jspinclude-tag) – Pino Mar 29 '19 at 11:14
  • Is that syntax correct i.e. – mkane Mar 29 '19 at 11:21
  • I tried with param as I said in the text, what end up happening is that the website breaks and nothing shows below the list, and still no list to be seen @soorapadman – MREugeneJ7 Mar 29 '19 at 11:27
  • Oops, sorry that was a typo in the post, in the code it's all gucci, thanks for pointing it out @mkane – MREugeneJ7 Mar 29 '19 at 11:29
  • @MREugeneJ7 Give a screenshot of your project structure and post the jsp page where you want to include another jsp page – Avijit Barua Mar 30 '19 at 06:49

3 Answers3

0

<jsp:include page="pagename.jsp" /> i thing you are not provide extension in your include tag

Kamal Kumar
  • 194
  • 12
  • Thanks for the answer, tried this already but it's not working for me either – MREugeneJ7 Mar 29 '19 at 12:40
  • https://stackoverflow.com/questions/9110148/include-another-jsp-file please follow this one may be help for you – Kamal Kumar Mar 29 '19 at 12:45
  • When I add teh stuff as a param my website just stops loading, it's not like it doesn't show now, it just shows but without the table I'm building because there's no way for me to pass the information, see in the edit how I'm doing the table. – MREugeneJ7 Mar 29 '19 at 12:56
0

Sometimes since I've had prior experience back in the days with HTML and PHP, we simply just would include things like navigation and header for easier management.

I'm unsure for what purpose you're including JSP, but here's an example of how I've done it since I include JSP if a boolean is true.

The site that is accessed:

<div class="row">
            <div class="col-lg-2">
            </div>
            <div class="col-lg-8">

                <%
                    if (loggedin) {
                %>

                <%@ include file="includes/profile.jsp" %>

                <%    } else {
                    out.println("<div><h2>You aren't logged in!</h2></div>");
                }
                %>

            </div>
            <div class="col-lg-2">
            </div>
        </div>

And the top and bottom of the JSP file I'm including doesn't contain things like head, title, html, body etc.

<%

    User currUser = null;

    currUser = (User) session.getAttribute("user");

%>


<div>
    <h2>Du er logget ind, velkommen <% out.println(currUser.getUsername().toUpperCase()); %></h2> <br>

    <h5>Din Saldo: <b><%  if (currUser.getBalance() < 0) { out.println("<font color='red'>" + currUser.getBalance() + "</font>");} else { out.println("<font color='green'>" + currUser.getBalance() + "</font>");}  %></b></h5>
    <br>
    <form class="form" method="post" action="#">
        <h4>Oplysninger: </h4>
        <h6>
            For at  &aelig;ndre oplysninger skal du indtaste dit kodeord!
        </h6>
        Nuv&aelig;rende Kode: <input type="password" class="form-control" placeholder="kodeord" name="password" required>
        <hr>
        Email: <input type="text" class="form-control" value="<% out.println(currUser.getEmail()); %>" name="email"> <br>
        Brugernavn: <input type="text" class="form-control" value="<% out.println(currUser.getUsername()); %>" name="username"> <br>
        <input type="submit" class="btn btn-default" value="Indsend Oplysninger" />
    </form>
</div>

When importing JSP into JSP, it's important to know that they will conflict if 1. Duplicate Local Variables. 2. Certain HTML tags aren't correct.

Your intended use seems very complicated to me since I'm not quite understanding it, sorry :3 but if you can't get it working consider to throw it to a session and if it's a one time use then remove it once you used it.

GodLess
  • 48
  • 8
0

This is working from dacades

<%@ include file="../scripts/standard_head_declaration.jsp" %>
CuriousDev
  • 400
  • 3
  • 11