我可以将变量从 JSP scriptlet 传递到 JSTL,但不能从 JSTL 传递到 JSP scriptlet,不会出现错误
以下代码会导致错误:
<c:set var="test" value="test1"/>
<%
String resp = "abc";
resp = resp + test;
pageContext.setAttribute("resp", resp);
%>
<c:out value="${resp}"/>
错误显示
"error a line 4: unknown symbol 'test'".
How do I pass test
from the JSTL code to the JSP scriptlet?
The following code causes an error:
<c:set var="test" value="test1"/>
<%
String resp = "abc";
resp = resp + test;
pageContext.setAttribute("resp", resp);
%>
<c:out value="${resp}"/>
The error says
"error a line 4: unknown symbol 'test'".
How do I pass test
from the JSTL code to the JSP scriptlet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
脚本是嵌入在页面代码中的原始java,如果您在脚本中声明变量,那么它们将成为嵌入在页面中的局部变量。
相比之下,JSTL 完全使用范围属性,无论是在
page
、request
或session
范围内。您需要重新编写您的 scriptlet,以将test
作为一个属性:如果您查看
的文档,您会发现您可以指定 < code>scope 为page
、request
或session
,默认为page
。更好的是,根本不要使用 scriptlet:它们会让小耶稣哭泣。
Scripts are raw java embedded in the page code, and if you declare variables in your scripts, then they become local variables embedded in the page.
In contrast, JSTL works entirely with scoped attributes, either at
page
,request
orsession
scope. You need to rework your scriptlet to fishtest
out as an attribute:If you look at the docs for
<c:set>
, you'll see you can specifyscope
aspage
,request
orsession
, and it defaults topage
.Better yet, don't use scriptlets at all: they make the baby jesus cry.
@skaffman 确定了这一点。他们各自生活在自己的环境中。但是,我不会考虑使用 scriptlet 作为解决方案。您希望避免它们。如果您只想连接 EL 中的字符串,并且您发现
+
运算符对于 EL 中的字符串失败(这是正确的),那么只需执行以下操作:或者如果
abc
是从另一个名为${resp}
的作用域变量获取,然后执行以下操作:@skaffman nailed it down. They live each in its own context. However, I wouldn't consider using scriptlets as the solution. You'd like to avoid them. If all you want is to concatenate strings in EL and you discovered that the
+
operator fails for strings in EL (which is correct), then just do:Or if
abc
is to obtained from another scoped variable named${resp}
, then do: