You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Andun Sameera <an...@gmail.com> on 2013/07/03 19:09:26 UTC
How to call Java Function from JavaScript
Hi All,
I have looked in to this topic in many places and found some ways. In this
particular scenario, I have used
https://cwiki.apache.org/confluence/display/WICKET/Calling+Wicket+from+Javascriptarticle
as the reference.
What I did in Java,
public class HomePage extends WebPage {
private static final long serialVersionUID = 1L;
public HomePage(final PageParameters parameters) {
super(parameters);
final AbstractDefaultAjaxBehavior behave = new
AbstractDefaultAjaxBehavior() {
protected void respond(final AjaxRequestTarget target) {
target.add(new Label("foo", "Yeah I was just called from
Javascript!"));
}
public void renderHead(Component component,IHeaderResponse
response){
String componentMarkupId = component.getMarkupId();
String callbackUrl = getCallbackUrl().toString();
response.render(JavaScriptHeaderItem.forScript("var
componentMarkupId='"+componentMarkupId+"'; var
callbackUrl='"+callbackUrl+"';","values"));
}
};
add(behave);
}
}
and my HomePage.html,
<!DOCTYPE HTML>
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js
"></script>
<script type="text/javascript">
$(function() {
var wcall = Wicket.Ajax.get({ u: '${callbackUrl}' + '' });
alert(wcall);
});
</script>
</body>
</html>
What I tried to do is call the get ajax method using the vars I have
initialized. But when my page loads, in the firebug console it says,
ReferenceError: Wicket is not defined
[Break On This Error]
var wcall = Wicket.Ajax.get({ u: '${callbackUrl}' + '' });
What has gone wrong here ?
Is there any other good way to call Java Function from Javascript?
Thanks!
--
Regards
Andun S.L. Gunawardana
Undergraduate
Department of Computer Science And Engineering
University of Moratuwa
Sri Lanka
Blog - http://www.insightforfuture.blogspot.com/
LinkedIn - http://www.linkedin.com/pub/andun-s-l-gunawardana/34/646/703
Twitter -http://twitter.com/AndunSLG
Re: How to call Java Function from JavaScript
Posted by skdhir <sa...@gmail.com>.
Hi andunslg,
I'm a newbie to Wicket and going through the same trouble.
Can you please end to end complete example ?
Thanks,
Sanat.
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-to-call-Java-Function-from-JavaScript-tp4660028p4671077.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to call Java Function from JavaScript
Posted by Andun Sameera <an...@gmail.com>.
Thanks Bas! I have fixed the problem.
On Thu, Jul 4, 2013 at 1:10 AM, Bas Gooren <ba...@iswd.nl> wrote:
> Wicket's ajax functions require you to load the wicket ajax javascript.
>
> This is handled by the AbstractDefaultAjaxBahavior out of the box.
> However, since you do not call super.renderHead(component,**response) in
> your code, the required javascript files are not loaded.
>
> Met vriendelijke groet,
> Kind regards,
>
> Bas Gooren
>
> Op 3-7-2013 19:09, schreef Andun Sameera:
>
> Hi All,
>>
>> I have looked in to this topic in many places and found some ways. In this
>> particular scenario, I have used
>> https://cwiki.apache.org/**confluence/display/WICKET/**
>> Calling+Wicket+from+**Javascriptarticle<https://cwiki.apache.org/confluence/display/WICKET/Calling+Wicket+from+Javascriptarticle>
>> as the reference.
>>
>> What I did in Java,
>>
>> public class HomePage extends WebPage {
>> private static final long serialVersionUID = 1L;
>>
>> public HomePage(final PageParameters parameters) {
>> super(parameters);
>>
>>
>> final AbstractDefaultAjaxBehavior behave = new
>> AbstractDefaultAjaxBehavior() {
>> protected void respond(final AjaxRequestTarget target) {
>> target.add(new Label("foo", "Yeah I was just called from
>> Javascript!"));
>> }
>>
>> public void renderHead(Component component,IHeaderResponse
>> response){
>>
>> String componentMarkupId = component.getMarkupId();
>> String callbackUrl = getCallbackUrl().toString();
>>
>> response.render(**JavaScriptHeaderItem.**forScript("var
>> componentMarkupId='"+**componentMarkupId+"'; var
>> callbackUrl='"+callbackUrl+"';**","values"));
>> }
>> };
>>
>> add(behave);
>>
>> }
>>
>> }
>>
>> and my HomePage.html,
>>
>> <!DOCTYPE HTML>
>> <html>
>> <body>
>> <script src="http://ajax.googleapis.**com/ajax/libs/jquery/1.6.1/**
>> jquery.min.js<http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js>
>> "></script>
>> <script type="text/javascript">
>> $(function() {
>> var wcall = Wicket.Ajax.get({ u: '${callbackUrl}' + '' });
>> alert(wcall);
>> });
>> </script>
>> </body>
>> </html>
>>
>> What I tried to do is call the get ajax method using the vars I have
>> initialized. But when my page loads, in the firebug console it says,
>>
>> ReferenceError: Wicket is not defined
>> [Break On This Error]
>>
>> var wcall = Wicket.Ajax.get({ u: '${callbackUrl}' + '' });
>>
>> What has gone wrong here ?
>>
>> Is there any other good way to call Java Function from Javascript?
>>
>> Thanks!
>>
>>
>
--
Regards
Andun S.L. Gunawardana
Undergraduate
Department of Computer Science And Engineering
University of Moratuwa
Sri Lanka
Blog - http://www.insightforfuture.blogspot.com/
LinkedIn - http://www.linkedin.com/pub/andun-s-l-gunawardana/34/646/703
Twitter -http://twitter.com/AndunSLG
Re: How to call Java Function from JavaScript
Posted by Bas Gooren <ba...@iswd.nl>.
Wicket's ajax functions require you to load the wicket ajax javascript.
This is handled by the AbstractDefaultAjaxBahavior out of the box.
However, since you do not call super.renderHead(component,response) in
your code, the required javascript files are not loaded.
Met vriendelijke groet,
Kind regards,
Bas Gooren
Op 3-7-2013 19:09, schreef Andun Sameera:
> Hi All,
>
> I have looked in to this topic in many places and found some ways. In this
> particular scenario, I have used
> https://cwiki.apache.org/confluence/display/WICKET/Calling+Wicket+from+Javascriptarticle
> as the reference.
>
> What I did in Java,
>
> public class HomePage extends WebPage {
> private static final long serialVersionUID = 1L;
>
> public HomePage(final PageParameters parameters) {
> super(parameters);
>
>
> final AbstractDefaultAjaxBehavior behave = new
> AbstractDefaultAjaxBehavior() {
> protected void respond(final AjaxRequestTarget target) {
> target.add(new Label("foo", "Yeah I was just called from
> Javascript!"));
> }
>
> public void renderHead(Component component,IHeaderResponse
> response){
>
> String componentMarkupId = component.getMarkupId();
> String callbackUrl = getCallbackUrl().toString();
>
> response.render(JavaScriptHeaderItem.forScript("var
> componentMarkupId='"+componentMarkupId+"'; var
> callbackUrl='"+callbackUrl+"';","values"));
> }
> };
>
> add(behave);
>
> }
>
> }
>
> and my HomePage.html,
>
> <!DOCTYPE HTML>
> <html>
> <body>
> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js
> "></script>
> <script type="text/javascript">
> $(function() {
> var wcall = Wicket.Ajax.get({ u: '${callbackUrl}' + '' });
> alert(wcall);
> });
> </script>
> </body>
> </html>
>
> What I tried to do is call the get ajax method using the vars I have
> initialized. But when my page loads, in the firebug console it says,
>
> ReferenceError: Wicket is not defined
> [Break On This Error]
>
> var wcall = Wicket.Ajax.get({ u: '${callbackUrl}' + '' });
>
> What has gone wrong here ?
>
> Is there any other good way to call Java Function from Javascript?
>
> Thanks!
>