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

时间:2011-05-03 15:48:51

标签: jsp jspinclude

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

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

a.jsp有一个我需要在c.jsp

中访问的Java对象

有没有办法在不将任何代码从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

1 个答案:

答案 0 :(得分:1)

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

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

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

**c.jsp**
w_myObject = (TypeOfMyObject)request.getAttribute("myObjectKey");
相关问题