打印java scriptlet变量,就好像它是一个JavaScript变量一样 [英] Printing a java scriptlet variable as if it is a JavaScript variable

查看:93
本文介绍了打印java scriptlet变量,就好像它是一个JavaScript变量一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我需要在jsp内的标签内的javascript调用中输出一个java变量!

hello i need to output a java variable inside a javascript call inside a tag inside a jsp !

例如:

 <% String param = "hello";%>

<dmf:checkbox  name="checkbox"
  onclick = "selectAll(<%=param%>)"
/>

生成的javascript是:

the javascript generated is:

selectAll(<%= param%>),this); 但我实际上想要像

selectAllCheckBoxes(Hello),this);


推荐答案

这不是逃避。那只是打印一个scriptlet变量,好像它是一个JavaScript变量。

That's not escaping. That's just printing a scriptlet variable as if it is a JavaScript variable.

此外,您的示例令人困惑,它们彼此不匹配,并且Javascript代码在语法上无效。我至少可以告诉JavaScript字符串变量被引号括起来。如果你想最终得到

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

selectAllCheckBoxes('Hello', this);

其中 Hello 应作为值获取scriptlet local name 变量( param 是保留变量名,你不应该自己使用它),然后你需要做

where Hello should be obtained as a value of the scriptlet local name variable (the param is a reserved variable name, you shouldn't use it yourself), then you need to do

selectAllCheckBoxes('<%= name %>', this);

以同样的方式,如果你想结束

In the same way, if you want to end up with

onclick="selectAll('Hello')"

你需要做什么

onclick="selectAll('<%= name %>')"

这就是说,我强烈建议你停止使用老式的小程序,因为这些小程序不仅仅是十年。建议JSP程序员仅使用taglib和EL来使JSP代码更干净,更健壮,更易于维护。您可以使用 JSTL 等标记库来控制流程在JSP页面中,您可以使用 EL 访问后端数据。您的示例可以替换为:

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:

<c:set var="name" value="Hello" />

...

selectAllCheckBoxes('${name}', this);

这篇关于打印java scriptlet变量,就好像它是一个JavaScript变量一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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