从Javascript调用Wicket 6 Code并返回值

时间:2018-04-12 05:59:29

标签: javascript java wicket wicket-6

我已设法在此示例中使用选项A从Javascript调用我的Wicket 6 Java代码:https://stackoverflow.com/a/42612027/1047418

但是,我无法找到将数据从Java端返回到JavaScript的示例(生成的JavaScript回调函数甚至不包含return语句)。如何实现这一目标?

编辑:我不是试图在Java中设置属性,正如我已经解释的那样,从JavaScript调用Wicket不是问题所在。我试图从Wicket返回一个JSON对象作为Ajax请求的结果返回到浏览器

Edit2:按照马丁的例子,我拼凑了这个工作实例......

爪哇

public class MyAjaxBehaviour extends AbstractDefaultAjaxBehavior {

    @Override
    protected void onComponentTag(ComponentTag tag) {
        super.onComponentTag(tag);
        tag.put("aprachatcallbackurl", getCallbackUrl());
    }

    @Override
    protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
        super.updateAjaxAttributes(attributes);
        attributes.setDataType("json");
        attributes.setWicketAjaxResponse(false);
    }

    @Override
    protected void respond(AjaxRequestTarget target) {
        getComponent().getRequestCycle().replaceAllRequestHandlers(
            new TextRequestHandler("application/json", "UTF-8", "{...JSON GOES HERE...}));
    }
}

的JavaScript

var mySuccessCallback = function(param1, param2, data, statusText) {
    // Data contains the parsed JSON object from MyAjaxBehaviour.respond(...)
    ...
}

var myFailureCallback = function() {
    ...
}

Wicket.Ajax.get({
    "u": callbackUrl,
    "dt": "json",
    "wr": false,
    "sh": [mySuccessCallback],
    "fh": [myFailureCallback]
});

主要问题是Wicket 7 Reference错误地指示使用" wr"而不是" dt"在JavaScript调用中。 :)

2 个答案:

答案 0 :(得分:2)

我认为你可以用更简单的方式做到这一点!

Wicket Ajax API只是:Wicket.Ajax.ajax({...})。您需要在服务器端准备的只是保存回调网址,例如将其全局保存在window对象或HTML元素的属性(data-the-url)中。

public class CallFromJavascriptBehavior extends AbstractDefaultAjaxBehavior {
   @Override
   protected void respond(AjaxRequestTarget target) {
      final StringValue parameterValue = RequestCycle.get().getRequest().getQueryParameters().getParameterValue("yourName");
      System.out.println(String.format("Hello %s", parameterValue.toString()));

      // write anything to the WebResponse and then consume it in the JS success handler. See below
   }

   @Override
   public void onComponenntTag(ComponenntTag tag, Component component) {
       super.onComponenntTag(tag, component);
       tag.put("data-the-url", getCallbackUrl());
   }
}

然后在你的JS代码中你可以这样做:

var callbackUrl = jQuery("#theElementId").data("the-url");
Wicket.Ajax.get({"u": callbackUrl, "sh":[successHandler], "fh": [failureHandler] });

其中successHandlerfailureHandler是内联定义的JS函数(例如function(...) {})或其他地方。

您可以在以下网站找到更多文档: https://ci.apache.org/projects/wicket/guide/7.x/single.html#_ajax_request_attributes_and_call_listeners

一篇博客文章,其中包含http://wicketinaction.com/2012/07/wicket-6-javascript-improvements/

的完整示例

答案 1 :(得分:1)

您可以编写一个资源并安装它,并使用您最喜欢的Ajax方法获取它。

例如:

public class MyResource extends AbstractResource
    @Override
    protected ResourceResponse newResourceResponse( Attributes attributes )
    {

        ResourceResponse resourceResponse = new ResourceResponse();
        resourceResponse.setContentType( "text/json" );
        resourceResponse.setTextEncoding( "utf-8" );

        HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();

        try
        {
            this.json = IOUtils.toString( request.getInputStream() );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }

        resourceResponse.setWriteCallback( new WriteCallback()
        {
            @Override
            public void writeData( Attributes attributes ) throws IOException
            {
                OutputStream outputStream = attributes.getResponse().getOutputStream();
                Writer writer = new OutputStreamWriter( outputStream );


                writer.write( MyResource.this.json );
                writer.close();
            }
        } );

        return resourceResponse;
    }

(来自我在https://stackoverflow.com/a/17876029/461499的其他答案)

请看这里安装它: https://dzone.com/articles/how-implement-rss-feeds-custom