打印 java scriptlet 变量,就好像它是 JavaScript 变量一样

发布于 2024-08-16 04:37:17 字数 358 浏览 5 评论 0原文

你好,我需要在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

居里长安 2024-08-23 04:37:17

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

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

selectAllCheckBoxes('Hello', this);

获得 Hello 作为 scriptlet 本地 name 变量的值(param 是保留变量名称) ,你不应该自己使用它),那么你需要做

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

同样的,如果你想最终

onclick="selectAll('Hello')"

你需要做

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

这就是说,我强烈建议你停止使用老式的 scriptlet,因为它是不鼓励的十多年了。 JSP 程序员被建议使用 taglibs 和 EL 只是为了使 JSP 代码更干净、更健壮并且更好的可维护性。您可以使用 JSTL 等标签库来控制流程在JSP页面中,您可以使用 EL 来访问“后端”数据。您的示例可以替换为:

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

...

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

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

selectAllCheckBoxes('Hello', this);

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')"

you need to do

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

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);
旧人 2024-08-23 04:37:17

使用 scriptlet 生成整个属性值,如下所示:

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

Generate the whole attribute value with a scriptlet, like this:

<dmf:checkbox  name="checkbox"
   onclick = "<%= "selectAll(" + param + ")" %>" />
锦上情书 2024-08-23 04:37:17

也许你正在努力实现这个目标?

var myVar = '<%= (String)request.getParameter("tab") %>'; 
loadtabs(myVar);

maybe you are trying to achieve this?

var myVar = '<%= (String)request.getParameter("tab") %>'; 
loadtabs(myVar);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文