简体   繁体   English

在CSS模板扩展中调用的Java方法中,将javascript变量用作参数

[英]Using a javascript variable as a parameter in a java method called in a template expansion within css

I'm trying to pass a variable pulled from a javascript file as a parameter for a java method in a template expansion. 我试图将从javascript文件提取的变量作为模板扩展中java方法的参数传递。 The variable is the file path of an image I want to display on the web page. 变量是我要在网页上显示的图像的文件路径。 When I try to display the plain image, I can call it using 当我尝试显示纯图像时,可以使用

"${ImageUrl}"

which returns the whole file. 返回整个文件。 However, the file is in an external filesystem, so what I want to do is call a method that can go fetch the correct file and handle that as necessary and pass the file path as the parameter in that method. 但是,该文件位于外部文件系统中,因此我想做的是call一个方法,该方法可以获取正确的文件并根据需要进行处理,并将文件路径作为该方法中的参数传递。 However, when I call 但是,当我call

"${getImage(ImageUrl}"

my java file reads ImageUrl as the text "ImageUrl". 我的Java文件将ImageUrl读取为文本“ ImageUrl”。

Is there a way to call the method and pass in the correct javascript variable as a parameter? 有没有办法调用该方法并将正确的javascript变量作为参数传递?

Thanks 谢谢

Keep in mind the basic physics of web apps: JavaScript in the browser can't directly call a method in Java code on the server. 请记住Web应用程序的基本原理:浏览器中的JavaScript不能直接在服务器上用Java代码调用方法。 Furthermore, with Tapestry, when you have a template expansion (the ${...} syntax) in a template file, that expansion is first resolved on the server side before the HTML is sent to the browser, THEN your JavaScript runs in the browser. 此外,使用Tapestry,如果您在模板文件中进行了模板扩展($ {...}语法),则首先在服务器端解析该扩展,然后再将 HTML发送至浏览器,然后您的JavaScript将在浏览器。

You didn't provide enough information for the reader to know what the best solution is in your case. 您没有提供足够的信息让读者知道您所遇到的最佳解决方案。 You could send info from JavaScript to the server-side web app via an AJAX call; 您可以通过AJAX调用将信息从JavaScript发送到服务器端网络应用; or maybe the JavaScript should request a whole page render, passing the image name as a page activation context; 也许JavaScript应该请求整个页面渲染,并将图像名称作为页面激活上下文传递; or the JavaScript could just build the actual URL itself and create an tag on the fly. 或者JavaScript可以自己构建实际的URL并即时创建标签。 It all depends on your own preferences and requirements. 这完全取决于您自己的喜好和要求。

Javascript files are static, they can be downloaded once and cached by the browser. Javascript文件是静态的,可以下载一次并由浏览器缓存。 Rather than embedding dynamic variables in your js file, pass them in as a function argument 与其将动态变量嵌入到js文件中,不如将它们作为函数参数传入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不使用参数作为参数的情况下更改方法中的布尔变量的值(Java) - Changing value of a boolean variable within a method without using it as a parameter (Java) 使用Groovy在Java属性中进行变量扩展 - Using Groovy for variable expansion in Java properties 在javascript中使用Java变量-webDriver - using java variable within javascript - webDriver Java中的可变压缩和扩展? - variable compression and expansion in java? 如何在作为不同构造函数初始值设定项的参数调用的方法中使用数组? (爪哇) - How can I use an array within a method that was called in as a parameter for a different constructor initializer?? (Java) 在方法中保护变量| 爪哇 - Protecting a variable within a method | Java Java:方法中变量的生命周期 - Java: lifecycle of a variable within a method 在Java中,可以在不使用数组的情况下在另一个方法的循环内调用/调用一个方法吗? - In java, can a method be called/invoked within a loop of another method without using arrays? 缺少方法参数类型[java.lang.Integer]的URI模板变量'studentId' - Spring MVC - Missing URI template variable 'studentId' for method parameter type [java.lang.Integer] - Spring MVC 从JPanel中使用javascript调用Java方法 - Calling a Java method using javascript from within a JPanel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM