À partir d'une JSP incluse, comment accéder à une variable déclarée dans une autre JSP incluse (même parent)

StackOverflow https://stackoverflow.com/questions/5872146

  •  28-10-2019
  •  | 
  •  

Question

J'ai une JSP parent avec un code qui ressemble à

<jsp:include page='a.jsp' flush='true'/>
<jsp:include page='b.jsp' flush='true'/>
<jsp:include page='c.jsp' flush='true'/>

a.jsp a un objet Java auquel j'ai besoin d'accéder dans c.jsp

Y a-t-il un moyen de faire cela sans déplacer le code de a.jsp vers le jsp parent?

Voici à quoi ressemble le a.jsp:

<%@ page import="com.xxx.yyy.myClass" %>
<%
    // Some processing here
%>
<table width="100%" cellspacing="0" class="scrollableTable">
    <thead>
        <tr>
        <%
            // Some processing here
            w_myObject = myAPI.getmyObject(param1, param2);
            // Some processing here
        %>
        </tr>
        <!-- Display contents of w_myObject in subsequent rows of this table, here -->
    </thead>
</table>

Et je veux accéder à w_myObject dans c.jsp

Était-ce utile?

La solution

This is all to do with scopes. If your Object is in Request scope then of course it will have access. Or if it is in Session scope it will have access. However, if it is in PageContext scope I believe it will be lost, as each jsp include creates its own scope.

So what I'm trying to say is put the Object in request scope and it will be visible across all JSPs.

**a.jsp**
request.setAttribute("myObjectKey", w_myObject);

**c.jsp**
w_myObject = (TypeOfMyObject)request.getAttribute("myObjectKey");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top