从一个包含的 JSP 中,我们如何访问另一个包含的 JSP(同一父级)中声明的变量

发布于 2024-11-05 10:11:38 字数 885 浏览 1 评论 0原文

我有一个父 JSP,其代码看起来像

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

a.jsp 有一个 Java 对象,我需要在 c.jsp 中访问该对象

有没有办法在不移动任何内容的情况下执行此操作从a.jsp到父jsp的代码?

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>

我想访问 c.jsp 中的 w_myObject

I have a parent JSP with code that looks like

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

a.jsp has a Java object which I need to access in c.jsp

Is there a way to do this without moving any code from a.jsp to the parent jsp?

Here is how the a.jsp looks like:

<%@ 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>

And I want to access w_myObject in c.jsp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子是时光的心 2024-11-12 10:11:38

这都与范围有关。如果您的对象在请求范围内,那么它当然可以访问。或者,如果它在会话范围内,它将具有访问权限。但是,如果它在 PageContext 范围内,我相信它将丢失,因为每个 jsp include 创建自己的范围。

所以我想说的是,将对象放入请求范围内,它将在所有 JSP 中可见。

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

**c.jsp**
w_myObject = (TypeOfMyObject)request.getAttribute("myObjectKey");

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文