在scriptlet<%XY%>中直接使用EL $ {XY} [英] Use EL ${XY} directly in scriptlet <% XY %>

查看:147
本文介绍了在scriptlet<%XY%>中直接使用EL $ {XY}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我每次打开JSP时都要对变量进行设置。
我在JSP和EL $ {} 中使用scriptlets < %%> 进行了尝试变量返回。

In my project I've to asign a variable every time the JSP is being opened. I tried it with scriptlets <% %> in JSP and EL ${} which gives the variable back.

但似乎无效。

 <% String korrekteAntwort=${frage.korrekteAntwort};%>
 <%session.setAttribute("korrekteAntwort", korrekteAntwort);%>

之后出现错误korrekteAntwort = $ {} ,是不是可以在scriptlet中直接从EL变换一个变量?

There is an error after korrekteAntwort=${}, Isn't it possible to asign directly an variable from EL in a scriptlet?

推荐答案

你正在混合 scriptlets 和EL,并期望它们同步运行。那是行不通的。 这是一种古老的编写JSP的方式另一种是编写JSP的现代方式。你应该使用其中一个,而不是两个。

You're mixing scriptlets and EL and expecting that they run "in sync". That just won't work. The one is an oldschool way of writing JSPs and the other is a modern way of writing JSPs. You should use the one or the other, not both.

回到具体问题,在引擎盖下,EL通过 PageContext#findAttribute( ) 。所以在 scriptlets中完全相同

Coming back to the concrete question, under the hoods, EL resolves variables by PageContext#findAttribute(). So just do exactly the same in scriptlets.

Frage frage = (Frage) pageContext.findAttribute("frage");
session.setAttribute("korrekteAntwort", frage.getKorrekteAntwort());

然而,如上所述,这是一种使用JSP的老式学习方式不一定是您考虑过的功能要求的最佳方式,但没有说什么。现代JSP方式将使用 JSTL < c:set>

However, as said, this is an oldschool way of using JSPs and not necessarily the "best" way for the functional requirement which you've had in mind, but didn't tell anything about. The modern JSP way would be using JSTL <c:set>:

<c:set var="korrekteAntwort" value="${frage.korrekteAntwort}" scope="session" />

这将在会话范围内以 $ {korrekteAntwort} 从该行开始,这正是 scriptlet 的行。

This will be available in session scope as ${korrekteAntwort} from that line on, which is exactly what that line of scriptlet does.

这篇关于在scriptlet&lt;%XY%&gt;中直接使用EL $ {XY}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆