【发布时间】:2012-10-13 12:08:35
【问题描述】:
更新:代码现在在 github 中,以及重现错误的说明(在 README.md > 说明重现 jstl - el - 从地图中读取元素)。有关我的 jsp 文件的格式,请参阅我的 other question。这个我还没想通
这行得通:
<%
HttpSession ses = (HttpSession) request.getSession(false);
if(ses != null) {
HashMap<String, String> m = (HashMap<String, String>) ses.getAttribute("messages");
if(m != null) { %>
<%=m.get(request.getParameter("r")) %>
<%}
}
%>
这不是:
<c:set var="key" value="${param.r}"/>
<c:out value="${key}" /> <!-- does print what I expect -->
<%-- ${sessionScope.messages} prints nothing --%>
sess scope : ${sessionScope} <%-- prints sess scope : {} --%>
<c:out value="${sessionScope.messages[key]}" /> <!-- nothing !? -->
为什么?
jstl-1.2.1 - 也尝试使用 1.2
relevant jsp(error_begin.jsp - 由 error_end.jsp 补充,仅包含结束 }):
<%@ page language="java" pageEncoding="UTF-8"%><!-- noway to include this ONCE -->
<%@page import="java.util.HashMap"%>
<% if(request.getAttribute("ErrorString") != null){ %>
Υπήρξε λάθος : ${requestScope.ErrorString}
<%} else { %>
<c:if test="${param.r != null}">
<div class="success" align="center">
<%
HttpSession ses = (HttpSession) request.getSession(false);
if(ses != null){
HashMap<String, String> m = (HashMap<String, String>) ses.getAttribute("messages");
if(m != null){ %>
<%=m.get(request.getParameter("r")) %>
<%}
}
%><c:set var="key" value="${param.r}"></c:set>
<%-- <c:out value="${key}" /> --%>
<%-- ${sessionScope.messages[key]} --%>
<%-- ${sessionScope.messages} prints nothing --%>
sess scope : ${sessionScope} <%-- prints {} --%>
<%-- <c:out value="${sessionScope.messages[key]}" /> --%>
<%-- <c:out value=" ${messages[param.r]}" /> --%>
</div>
</c:if>
<c:forEach items="${messages}" var="entry">
Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>
Web.xml 的相关部分:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
joda-time-2.1-javadoc.jar
joda-time-2.1.jar
logback-classic-1.0.7.jar
logback-core-1.0.7.jar
mail.jar
mysql-connector-java-5.1.22-bin.jar
slf4j-api-1.7.2-javadoc.jar
slf4j-api-1.7.2-sources.jar
slf4j-api-1.7.2.jar
【问题讨论】:
-
您尝试过使用 ${messages[param.r]} 吗?
-
@rickz:是的,这就是我最终设置一个新变量的原因。会不会是bug?
-
想解释为什么投反对票?这实际上是一个非常具体和准确的问题——这是怎么回事?
-
${sessionScope}说什么?还有${sessionScope.messages}?请注意,您不一定需要为此使用<c:out>,模板文本中的 EL 也可以正常工作(如果没有,您会遇到严重的环境问题)。 -
@BalusC : 编辑了问题:)
标签: jsp jstl tomcat7 el servlet-3.0