打印 java scriptlet 变量,就好像它是 JavaScript 变量一样
你好,我需要在jsp内的标签内的javascript调用内输出一个java变量!
例如:
<% String param = "hello";%>
<dmf:checkbox name="checkbox"
onclick = "selectAll(<%=param%>)"
/>
生成的javascript是:
selectAll(<%=param%>),this);
但我实际上想要类似的东西
selectAllCheckBoxes(Hello),this);
hello i need to output a java variable inside a javascript call inside a tag inside a jsp !
for example:
<% String param = "hello";%>
<dmf:checkbox name="checkbox"
onclick = "selectAll(<%=param%>)"
/>
the javascript generated is:
selectAll(<%=param%>),this);
but I actually want something like
selectAllCheckBoxes(Hello),this);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是逃避。这只是打印一个 scriptlet 变量,就好像它是一个 JavaScript 变量一样。
此外,您的示例令人困惑,它们彼此不匹配,并且 Javascript 代码在语法上无效。我至少可以告诉 JavaScript 字符串变量要用引号括起来。如果您想最终
获得
Hello
作为 scriptlet 本地name
变量的值(param
是保留变量名称) ,你不应该自己使用它),那么你需要做同样的,如果你想最终
你需要做
这就是说,我强烈建议你停止使用老式的 scriptlet,因为它是不鼓励的十多年了。 JSP 程序员被建议使用 taglibs 和 EL 只是为了使 JSP 代码更干净、更健壮并且更好的可维护性。您可以使用 JSTL 等标签库来控制流程在JSP页面中,您可以使用 EL 来访问“后端”数据。您的示例可以替换为:
That's not escaping. That's just printing a scriptlet variable as if it is a JavaScript variable.
Besides, your examples are confusing, they doesn't match each other and the Javascript code is syntactically invalid. I can at least tell that JavaScript string variables are to be surrounded by quotes. If you want to end up with
where
Hello
should be obtained as a value of the scriptlet localname
variable (theparam
is a reserved variable name, you shouldn't use it yourself), then you need to doIn the same way, if you want to end up with
you need to do
That said, I strongly recommend you to stop using the old fashioned scriptlets which are been discouraged since more than a decade. JSP programmers were been recommended to use taglibs and EL only to make the JSP code more clean and robust and better maintainable. You can use taglibs such as JSTL to control the flow in the JSP page and you can use EL to access the "back-end" data. Your example can be replaced by:
使用 scriptlet 生成整个属性值,如下所示:
Generate the whole attribute value with a scriptlet, like this:
也许你正在努力实现这个目标?
maybe you are trying to achieve this?