简体   繁体   English

如何在Java中获取Javascript var值(wicket 6)

[英]How to get Javascript var value in Java (wicket 6)

I have a fairly simple question, but I'm unable to find a working answer. 我有一个很简单的问题,但找不到有效的答案。 I'm using javascript in my HTML file for a page that is taking input from the page and giving me back a string of a jSON object. 我在HTML文件中使用了javascript,这是一个页面的页面,该页面从页面获取输入并给我一个jSON对象的字符串。 All that I want to do is take this string and put it in a Java string. 我要做的就是将这个字符串放入Java字符串中。 I can do things like 我可以做类似的事情

target.appendJavaScript("s=getString();");
target.appendJavaScript("alert(s);")

which give me the desired information in the alert. 在警报中向我提供所需的信息。 But how do I get s into a Java string? 但是,如何将其转换为Java字符串?

I have tried the following 我尝试了以下

StringValue temp = RequestCycle.get().getRequest().getQueryParameters().getParameterValue("s");
info(temp.toString());

And other variations like getRequestParameters(), but I get nulls on temp. 以及其他类似getRequestParameters()的变体,但我在temp上得到null。 These actions are inside an AjaxFallbackButton's onSubmit. 这些动作在AjaxFallbackButton的onSubmit内部。

Any advice on how to get a javascript var in Java? 关于如何在Java中获取javascript var的任何建议?

Although I would still like to know more about sharing variables between Java and Javascript in HTML, my case was solved thanks to Wicket. 尽管我仍然想了解有关在Java和HTML中的Java脚本之间共享变量的更多信息,但由于有了Wicket,我的情况才得以解决。 The value that I was retrieving with s=getString() was already accessible in a HiddenField, and so I simply needed to get it a wicket:id in the HTML and add it with a property model in the Java. 我使用s = getString()检索的值已经可以在HiddenField中访问,因此我只需要在HTML中为其获取一个wicket:id并在Java中将其与属性模型一起添加即可。 Hopefully this might help someone else in the future. 希望这可能会在将来对其他人有所帮助。

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

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