无法访问包含的JSP内的变量

Sun*_*van 1 spring jsp jstl el

我在我的应用程序中使用Spring 2.5.在视图中我有主jsp,其中我包含了另一个jsp.我在主jsp中使用c:set标签声明了一个变量,我无法在jsp中访问它.下面是代码main.jsp

<c:set var="hPediquestStudy"><spring:message code="study.hpediquest.mapping" /></c:set>
<c:set var="currentStudy" value='<%=(String)session.getAttribute("currentStudy")%>'/>
<html>
<head>
</head>
<body>
<c:if test="${currentStudy eq hPediquestStudy}">
  Variables are equal
</c:if>
<c:if test="${currentStudy ne hPediquestStudy}">
  Variables are not equal
</c:if>
<jsp:include page="/WEB-INF/jsp/included.jsp"></jsp:include>
</body
</html>
Run Code Online (Sandbox Code Playgroud)

included.jsp

<c:if test="${currentStudy eq hPediquestStudy}">
   hPediquestStudy Variable is accessible
</c:if>
 <br/>currentStudy : ${currentStudy}
 <br/>hPediquestStudy : ${hPediquestStudy}
Run Code Online (Sandbox Code Playgroud)

我正在输出

变量是平等的

currentStudy:hPediquest

hPediquestStudy:

  1. 为什么在主jsp上两个值都相等而在包含jsp中我看不到它的值?
  2. 为什么currentStudy在包含的jsp中显示其值?
  3. 是否有任何解决方案可以帮助我访问父jsp中的变量集ans可以在包含的jsps中访问?

如果我在主jsp中包含jsp中设置该变量,我可以看到hPediquestStudy值的值.但是每次我加入jsp时我都不想设置它.请帮忙

Bal*_*usC 12

为什么在主jsp上两个值都相等而在包含jsp中我看不到它的值?

因为<c:set>默认情况下将它们存储在页面范围中.


为什么currentStudy在包含的jsp中显示其值?

因为它也可用作会话属性.


是否有任何解决方案可以帮助我访问父jsp中的变量集ans可以在包含的jsps中访问?

您需要将scope属性设置<c:set>request或更高.(缺省)page作用域仅向当前JSP公开,而不是任何包含的JSP.


注意这一行

<c:set var="currentStudy" value='<%=(String)session.getAttribute("currentStudy")%>'/> 
Run Code Online (Sandbox Code Playgroud)

必要.在${currentStudy}将已扫描的页面,请求,会话和应用范围的变量.由于您显然已将其设置在会话范围内,因此无需将其复制到页面范围中.所以,只需删除该行.总而言之,您的前两<c:set>行应该替换为这一行:

<c:set var="hPediquestStudy" scope="request"><spring:message code="study.hpediquest.mapping" /></c:set>
Run Code Online (Sandbox Code Playgroud)

然后它会像你想象的那样工作.

也可以看看: