(Apache Wicket) 从 js 函数设置 java 属性

(Apache Wicket) Set java atrribute from a js function

我是 Apache Wicket 的新手,我需要在 Java 属性上设置值。该值来自 JS 上的 var,由特定 GIS 库 (https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html) 中的特定函数填充。此设置必须由某些组件行为触发。

这是一个简化的示例代码:

检票口网页:

public class MapPage extends WebPage {

private static final long serialVersionUID = 1L;
private Integer coordinates;

// getters and setters

}

检票口 html:

<html xmlns:wicket="http://wicket.apache.org">
<head>

<!-- metas, scripts, and css imports -->
</head>

<body>
<script>
// component declarations

var coordinates = ''

map.on('draw:edited', function (e) {    

  e.layers.eachLayer(function(layer) {
    coordinates = toWKT(layer);
    // send coordinates to coordinates java attribute ??? how?? 
  });
});

</script>
</body>

非常感谢!

这是我的一个项目中的一段代码,我想在其中处理对 (HighCharts) 图表的点击。它将数据传递给 Wicket,然后 Wicket 更新另一个面板以显示与点击相关的详细信息。

相关的javascript部分,其中interactionurl实际上是后面行为生成的callbackScript:

  interactionurl(JSON.stringify(myDataToPass));

行为:

    this.add( this.interactionbehavior = new AbstractDefaultAjaxBehavior()
    {

        @Override
        protected void respond( final AjaxRequestTarget target )
        {
            RequestCycle cycle = RequestCycle.get();
            WebRequest webRequest = (WebRequest) cycle.getRequest();
            String param1 = webRequest.getQueryParameters().getParameterValue( "mydict" ).toString( "" );
            //param1 contains the JSON map passed from javascript.
            //you can also do stuff now, like replacing components using ajax
        }

        @Override
        protected void updateAjaxAttributes( AjaxRequestAttributes attributes )
        {
            super.updateAjaxAttributes( attributes );
            attributes.getExtraParameters().put( "mydict", "__PLACEHOLDER__" );
        }

        @Override
        public CharSequence getCallbackScript()
        {
            String script = super.getCallbackScript().toString().replace( "\"__PLACEHOLDER__\"", "data" );
            return script;
        }
    } );

您只需要在某个时刻将交互 url 传递到页面即可。为此,您可以在具有以下行为的组件中使用 renderHead

    @Override
    public void renderHead( final IHeaderResponse response )
    {
 ...
                //use the `setupCallback` to store the callback script somewhere.., I store it in 'interactionurl'
        String script = String.format(  " setupCallback(this.interactionbehavior.getCallbackScript()); "); 
        response.render( OnDomReadyHeaderItem.forScript( script ) 
    }