简体   繁体   English

将参数从Java传递给Tapestry中的js

[英]Pass parameter from java to js in Tapestry

I use Apache Tapestry as web application framework. 我使用Apache Tapestry作为Web应用程序框架。

I have variable in my java code. 我的Java代码中有变量。 I need value of this variable in javascript after page loaded. 页面加载后,我需要在javascript中此变量的值。

For example java class: 例如java类:

@Import(library = "RoomManagement.js")
public final class RoomManagement{
  @Property
  private long contactId;
}

and js in RoomManagement.js: 和RoomManagement.js中的js:

window.onload = function(){
    alert(contactId);
}

How can I pass it directly to javascript? 如何将其直接传递给javascript?

I can not pass value to js like to template, cause it is .js file not .tml. 我无法像模板一样将值传递给js,因为它是.js文件而不是.tml。

I can add invisible tag to my page, write value to this tag and read it from js. 我可以在页面上添加不可见标签,将值写入此标签并从js读取。 But do you know another way? 但是你知道另一种方式吗?

You will need to use the JavaScriptSupport service. 您将需要使用JavaScriptSupport服务。

Your java file: 您的Java文件:

@Import(library = "RoomManagement.js")
public final class RoomManagement{

  @Inject
  private JavaScriptSupport javascriptSupport;

  @Property
  private long contactId;

  @AfterRender
  private void setup() {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("contactId", contactId);
    javascriptSupport.addInitializerCall("RoomManagement",jsonObject);
  }
}

Your RoomManagement.js 您的RoomManagement.js

Tapestry.Initializer.RoomManagement = function (parameters) {
    //do whatever you need here
    alert('Your contactId: ' + parameters.contactId);
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM