(Apache Wicket) Set java atrribute from a js function

cassioramon

I am brand new on Apache Wicket and I need to set value on a Java attribute. This value comes from a var on JS filled by a specific function from a specific GIS lib (https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html). This setting must be triggered by some component behavior.

Here is a simplified example code:

Wicket web page:

public class MapPage extends WebPage {

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

// getters and setters

}

Wicket 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>

Thanks a lot!

RobAu

This is a piece of code from one of my projects, where I want to handle a click on a (HighCharts) chart. It passes data to Wicket and Wicket then updates another panel to display details related to the click.

The relevant javascript part, where interactionurl is actually the callbackScript that is generated by the behavior later on:

  interactionurl(JSON.stringify(myDataToPass));

The behaviour:

    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;
        }
    } );

You only need to pass the interaction url to the page on some moment. For this you can use renderHead in the component that has the behaviour:

    @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 ) 
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Set focus on a component with Apache Wicket?

remove Atrribute "hidden" from a table using DOM shows removeAttribute is not a function

Java: How to set a value from javascript to a wicket component textfield

Apache Wicket - Getting the cursor position from TinyMceBehavior

Wicket - Set Model from another panel

Apache wicket Tomcat Problem: java.lang.ClassNotFoundException: org.apache.wicket.protocol.http.WicketFilter

How to copy href atrribute by clipboard.js

How can I correctly remove an AjaxSelfUpdatingTimerBehavior from a component in Apache Wicket?

How to retrieve user name from CAS in Apache Wicket

Return Tree Set from function Java

What are the disadvantages of Apache Wicket?

Apache Wicket textfield onUpdate

Apache Wicket Repeaters: an overview

Populating table with apache wicket

Apache Wicket DateTextField clear

What are the advantages of Apache Wicket?

Jacoco support for apache wicket?

Apache Wicket 9.1 CSRF

Call javascript function from wicket 6, Link's "onclick ()"

Rhino: How to call JS function from Java

Crosswalk call js function from java on android

Calling server js function on mongodb from java

how to set property of JSON in data-atrribute (HTML5)

What does the "weights" atrribute do in the gwr function in R?

How do I call Java code from JavaScript code in Wicket?

Java/Wicket - How to stop browsers from caching pages?

React.js Hooks Set State Function From Event Listener

Js, what is 'this' when set the static function from a Class to variable object

Getting an infinite loop from set function in ember.js