如何从Wicket中的JavaScript代码调用Java代码?

时间:2021-12-19 06:51:47

If I can do this, how do I call Java code (methods for instance) from within JavaScript code, in Wicket.

如果我能做到这一点,我如何在Wicket中的JavaScript代码中调用Java代码(例如方法)。

4 个解决方案

#1


14  

erk. The correct answer would be ajax call backs. You can either manually code the js to hook into the wicket js, or you can setup the callbacks from wicket components in java. For example, from AjaxLazyLoadPanel:

ERK。正确的答案是ajax回拨。您可以手动编写js以挂接到wicket js,也可以从java中的wicket组件设置回调。例如,来自AjaxLazyLoadPanel:

        component.add( new AbstractDefaultAjaxBehavior() {

        @Override
        protected void respond(AjaxRequestTarget target) {
            // your code here
        }

        @Override
        public void renderHead(IHeaderResponse response) {
            super.renderHead( response );
            response.renderOnDomReadyJavascript( getCallbackScript().toString() );
        }

        }

This example shows how to add call back code to any Component in Wicket. After the OnDomReady event fires in your browser, when loading a page, Wicket will cause it's js enging, to call back into your code, using Ajax, to the 'respond' method shown above, at which point you can execute Java code on the server, and potentially add components to the ajax target to be re-rendered.

此示例显示如何将回调代码添加到Wicket中的任何Component。在您的浏览器中触发OnDomReady事件后,在加载页面时,Wicket将使其成为js enging,使用Ajax回调您的代码到上面显示的“响应”方法,此时您可以在服务器,并可能将组件添加到要重新呈现的ajax目标。

To do it manually, from js, you can hook into wicket's system by printing out getCallbackScript().toString() to a attribute on a wicket component, which you'll then be able to access from js. Calling this url from js manually with wicket's wicketAjaxGet from wicket-ajax.js.

要从js手动执行,您可以通过将getCallbackScript()。toString()打印到wicket组件上的属性来挂钩到wicket的系统,然后您可以从js访问该属性。从wicket-ajax.js中使用wicket的wicketAjaxGet手动调用js。

Check out the mailing list for lot's of conversation on this topic: http://www.nabble.com/Wicket-and-javascript-ts24336438.html#a24336438

查看关于此主题的大量对话的邮件列表:http://www.nabble.com/Wicket-and-javascript-ts24336438.html#a24336438

#2


5  

Excerpt from https://cwiki.apache.org/WICKET/calling-wicket-from-javascript.html

摘录自https://cwiki.apache.org/WICKET/calling-wicket-from-javascript.html

If you add any class that extends AbstractDefaultAjaxBehavior to your page, wicket-ajax.js will be added to the header ofyour web page. wicket-ajax.js provides you with two basic methods to call your component:

如果您添加任何将AbstractDefaultAjaxBehavior扩展到您的页面的类,wicket-ajax.js将被添加到您的网页的标题中。 wicket-ajax.js为您提供了两种调用组件的基本方法:

function wicketAjaxGet(url, successHandler, failureHandler, precondition, channel)

and

function wicketAjaxPost(url, body, successHandler, failureHandler, precondition, channel)

Here is an example:

这是一个例子:

JavaScript

function callWicket() {
   var wcall = wicketAjaxGet('$url$' + '$args$', function() { }, function() { });
}

$url$ is obtained from the method abstractDefaultAjaxBehavior.getCallbackUrl(). If you paste the String returned from that method into your browser, you'll invoke the respond method, the same applies for the javascript method.

$ url $是从abstractDefaultAjaxBehavior.getCallbackUrl()方法获得的。如果将从该方法返回的String粘贴到浏览器中,则将调用respond方法,这同样适用于javascript方法。

You can optionally add arguments by appending these to the URL string. They take the form &foo=bar.

您可以选择通过将这些参数附加到URL字符串来添加参数。它们采用&foo = bar的形式。

you get the optional arguments in the Java response method like this:

你在Java响应方法中得到可选参数,如下所示:

Map map = ((WebRequestCycle) RequestCycle.get()).getRequest().getParameterMap();

or this:

String paramFoo = RequestCycle.get().getRequest().getParameter("foo");

#3


4  

http://www.wicket-library.com/wicket-examples-6.0.x/index.html/ has plenty of examples to get you going.

http://www.wicket-library.com/wicket-examples-6.0.x/index.html/有很多例子可以帮助你。

Or have a Have a look at DWR

或者看看DWR

http://directwebremoting.org/

DWR allows Javascript in a browser to interact with Java on a server and helps you manipulate web pages with the results.

DWR允许浏览器中的Javascript与服务器上的Java交互,并帮助您使用结果操作网页。

As Dorward mentioned this is done via AJAX

正如Dorward所说,这是通过AJAX完成的

#4


0  

Assuming you mean JavaScript running on the client - you cause an HTTP redirect to be made to the server, and have your servlet react to the request for the given URL.

假设您的意思是在客户端上运行JavaScript - 您将导致对服务器进行HTTP重定向,并让您的servlet对给定URL的请求作出反应。

This is known as Ajax, and there are a number of libraries that help you do it..

这被称为Ajax,并且有许多库可以帮助您实现它。

#1


14  

erk. The correct answer would be ajax call backs. You can either manually code the js to hook into the wicket js, or you can setup the callbacks from wicket components in java. For example, from AjaxLazyLoadPanel:

ERK。正确的答案是ajax回拨。您可以手动编写js以挂接到wicket js,也可以从java中的wicket组件设置回调。例如,来自AjaxLazyLoadPanel:

        component.add( new AbstractDefaultAjaxBehavior() {

        @Override
        protected void respond(AjaxRequestTarget target) {
            // your code here
        }

        @Override
        public void renderHead(IHeaderResponse response) {
            super.renderHead( response );
            response.renderOnDomReadyJavascript( getCallbackScript().toString() );
        }

        }

This example shows how to add call back code to any Component in Wicket. After the OnDomReady event fires in your browser, when loading a page, Wicket will cause it's js enging, to call back into your code, using Ajax, to the 'respond' method shown above, at which point you can execute Java code on the server, and potentially add components to the ajax target to be re-rendered.

此示例显示如何将回调代码添加到Wicket中的任何Component。在您的浏览器中触发OnDomReady事件后,在加载页面时,Wicket将使其成为js enging,使用Ajax回调您的代码到上面显示的“响应”方法,此时您可以在服务器,并可能将组件添加到要重新呈现的ajax目标。

To do it manually, from js, you can hook into wicket's system by printing out getCallbackScript().toString() to a attribute on a wicket component, which you'll then be able to access from js. Calling this url from js manually with wicket's wicketAjaxGet from wicket-ajax.js.

要从js手动执行,您可以通过将getCallbackScript()。toString()打印到wicket组件上的属性来挂钩到wicket的系统,然后您可以从js访问该属性。从wicket-ajax.js中使用wicket的wicketAjaxGet手动调用js。

Check out the mailing list for lot's of conversation on this topic: http://www.nabble.com/Wicket-and-javascript-ts24336438.html#a24336438

查看关于此主题的大量对话的邮件列表:http://www.nabble.com/Wicket-and-javascript-ts24336438.html#a24336438

#2


5  

Excerpt from https://cwiki.apache.org/WICKET/calling-wicket-from-javascript.html

摘录自https://cwiki.apache.org/WICKET/calling-wicket-from-javascript.html

If you add any class that extends AbstractDefaultAjaxBehavior to your page, wicket-ajax.js will be added to the header ofyour web page. wicket-ajax.js provides you with two basic methods to call your component:

如果您添加任何将AbstractDefaultAjaxBehavior扩展到您的页面的类,wicket-ajax.js将被添加到您的网页的标题中。 wicket-ajax.js为您提供了两种调用组件的基本方法:

function wicketAjaxGet(url, successHandler, failureHandler, precondition, channel)

and

function wicketAjaxPost(url, body, successHandler, failureHandler, precondition, channel)

Here is an example:

这是一个例子:

JavaScript

function callWicket() {
   var wcall = wicketAjaxGet('$url$' + '$args$', function() { }, function() { });
}

$url$ is obtained from the method abstractDefaultAjaxBehavior.getCallbackUrl(). If you paste the String returned from that method into your browser, you'll invoke the respond method, the same applies for the javascript method.

$ url $是从abstractDefaultAjaxBehavior.getCallbackUrl()方法获得的。如果将从该方法返回的String粘贴到浏览器中,则将调用respond方法,这同样适用于javascript方法。

You can optionally add arguments by appending these to the URL string. They take the form &foo=bar.

您可以选择通过将这些参数附加到URL字符串来添加参数。它们采用&foo = bar的形式。

you get the optional arguments in the Java response method like this:

你在Java响应方法中得到可选参数,如下所示:

Map map = ((WebRequestCycle) RequestCycle.get()).getRequest().getParameterMap();

or this:

String paramFoo = RequestCycle.get().getRequest().getParameter("foo");

#3


4  

http://www.wicket-library.com/wicket-examples-6.0.x/index.html/ has plenty of examples to get you going.

http://www.wicket-library.com/wicket-examples-6.0.x/index.html/有很多例子可以帮助你。

Or have a Have a look at DWR

或者看看DWR

http://directwebremoting.org/

DWR allows Javascript in a browser to interact with Java on a server and helps you manipulate web pages with the results.

DWR允许浏览器中的Javascript与服务器上的Java交互,并帮助您使用结果操作网页。

As Dorward mentioned this is done via AJAX

正如Dorward所说,这是通过AJAX完成的

#4


0  

Assuming you mean JavaScript running on the client - you cause an HTTP redirect to be made to the server, and have your servlet react to the request for the given URL.

假设您的意思是在客户端上运行JavaScript - 您将导致对服务器进行HTTP重定向,并让您的servlet对给定URL的请求作出反应。

This is known as Ajax, and there are a number of libraries that help you do it..

这被称为Ajax,并且有许多库可以帮助您实现它。