问题描述
以下代码导致错误:
为什么我不能在 scriptlet 中使用表达式语言${test}"?
JSTL 变量实际上是属性,默认情况下范围在页面上下文级别.
因此,如果您需要访问 scriptlet 中的 JSTL 变量值,您可以通过调用 getAttribute()
方法在适当范围的对象上(通常是 pageContext
和请求).
resp = resp + (String)pageContext.getAttribute("test");
完整代码
但是为什么我会想到这个例外.
一个 JSP scriptlet 用于包含任何代码片段这对页面中使用的脚本语言有效.scriptlet 的语法如下:
当脚本语言设置为Java时,一个scriptlet被转换成Java编程语言的语句片段,并插入到JSP页面的servlet的服务方法中.
在 scriptlet 中,您可以编写 Java 代码,而 ${test}
可以用非 Java 代码编写.
不相关
The following code causes an error:
<c:set var="test" value="test1"/>
<%
String resp = "abc";
resp = resp + ${test}; //in this line I got an Exception.
out.println(resp);
%>
Why can't I use the expression language "${test}" in the scriptlet?
JSTL variables are actually attributes, and by default are scoped at the page context level.
As a result, if you need to access a JSTL variable value in a scriptlet, you can do so by calling the getAttribute()
method on the appropriately scoped object (usually pageContext
and request).
resp = resp + (String)pageContext.getAttribute("test");
Full code
<c:set var="test" value="test1"/>
<%
String resp = "abc";
resp = resp + (String)pageContext.getAttribute("test"); //No exception.
out.println(resp);
%>
A JSP scriptlet is used to contain any code fragment that is valid for the scripting language used in a page. The syntax for a scriptlet is as follows:
<%
scripting-language-statements
%>
When the scripting language is set to Java, a scriptlet is transformed into a Java programming language statement fragment and is inserted into the service method of the JSP page’s servlet.
In scriptlets you can write Java code and ${test}
in not Java code.
Not related
这篇关于在 Scriptlet 中访问 JSTL/EL 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!