首页 技术 正文
技术 2022年11月15日
0 收藏 673 点赞 3,014 浏览 4653 个字

1. java通过jsni调用内部js

  1. Button button = new Button(“java调用内部jsni的js方法”);
  2. button.addClickHandler(new ClickHandler() {
  3. @Override
  4. public void onClick(ClickEvent event) {
  5. //gwt中java调用js方法
  6. execute(“js方法被调用”);
  7. }
  8. });
  9. /**
  10. * JSNI方法
  11. * @param id
  12. */
  13. public static native void execute(String str) /*-{
  14. alert(str);
  15. }-*/;

2. 内部js通过jsni调用java方法

  1. Button button1 = new Button(“内部jsni的js调用java方法”);
  2. button1.addClickHandler(new ClickHandler() {
  3. @Override
  4. public void onClick(ClickEvent event) {
  5. //gwt中java调用js方法
  6. executeJs(“java方法被调用”);
  7. }
  8. });
  9. /**
  10. * JSNI方法,  里面调用java方法 javaAlert
  11. * @param id
  12. */
  13. public static native void executeJs(String str) /*-{
  14. @com.hw.client.TestCall::javaAlert(Ljava/lang/String;)(str);
  15. }-*/;

3.gwt中java方法调用外部js

在gwt工程的index.html中加入外部方法

  1. <mce:script language=”JavaScript”><!–
  2. function callOutJs(str){
  3. alert(‘此处是外部js方法:’+ str);
  4. }
  5. // –></mce:script>

然后在onModuleLoad中java方法进行调用

  1. Button button2 = new Button(“JAVA调用外部js”);
  2. button2.addClickHandler(new ClickHandler() {
  3. @Override
  4. public void onClick(ClickEvent event) {
  5. //gwt中java调用js方法
  6. callOutJS(“外部js被调用”);
  7. }
  8. });
  9. /**
  10. * JSNI方法 调用外部js方法
  11. * @param id
  12. */
  13. public static native void callOutJS(String str) /*-{
  14. $wnd.callOutJs(str);
  15. }-*/;

4.  外部js调用gwt的java方法

在onModuleLoad方法中调用  outJsCallGwt();

outJsCallGwt方法为

  1. /**
  2. * 需要被调用的js方法
  3. * @param id
  4. */
  5. private static native void outJsCallGwt() /*-{
  6. $wnd.outJsCallGwt = function (str) {
  7. alert(“此处是gwt:”+ str);
  8. };
  9. }-*/;

在index.html中加入按钮以调用

  1. <button onclick=”outJsCallGwt(‘外部按钮被点击’)”>点击</button>

现贴出application和index.html代码

  1. package com.hw.client;
  2. import com.google.gwt.core.client.EntryPoint;
  3. import com.google.gwt.event.dom.client.ClickEvent;
  4. import com.google.gwt.event.dom.client.ClickHandler;
  5. import com.google.gwt.user.client.Window;
  6. import com.google.gwt.user.client.ui.Button;
  7. import com.google.gwt.user.client.ui.RootPanel;
  8. public class TestCall implements EntryPoint {
  9. public void onModuleLoad() {
  10. Button button = new Button(“java调用内部jsni的js方法”);
  11. button.addClickHandler(new ClickHandler() {
  12. @Override
  13. public void onClick(ClickEvent event) {
  14. //gwt中java调用js方法
  15. execute(“js方法被调用”);
  16. }
  17. });
  18. Button button1 = new Button(“内部jsni的js调用java方法”);
  19. button1.addClickHandler(new ClickHandler() {
  20. @Override
  21. public void onClick(ClickEvent event) {
  22. //gwt中java调用js方法
  23. executeJs(“java方法被调用”);
  24. }
  25. });
  26. Button button2 = new Button(“JAVA调用外部js”);
  27. button2.addClickHandler(new ClickHandler() {
  28. @Override
  29. public void onClick(ClickEvent event) {
  30. //gwt中java调用js方法
  31. callOutJS(“外部js被调用”);
  32. }
  33. });
  34. RootPanel.get().add(button);
  35. RootPanel.get().add(button1);
  36. RootPanel.get().add(button2);
  37. outJsCallGwt();
  38. }
  39. /**
  40. * JSNI方法 调用外部js方法
  41. * @param id
  42. */
  43. public static native void callOutJS(String str) /*-{
  44. $wnd.callOutJs(str);
  45. }-*/;
  46. /**
  47. * JSNI方法
  48. * @param id
  49. */
  50. public static native void execute(String str) /*-{
  51. alert(str);
  52. }-*/;
  53. /**
  54. * JSNI方法,  里面调用java方法 javaAlert
  55. * @param id
  56. */
  57. public static native void executeJs(String str) /*-{
  58. @com.hw.client.TestCall::javaAlert(Ljava/lang/String;)(str);
  59. }-*/;
  60. /**
  61. * 被js方法调用
  62. * @param id
  63. */
  64. public static void javaAlert(String str){
  65. Window.alert(str);
  66. }
  67. /**
  68. * 需要被调用的js方法
  69. * @param id
  70. */
  71. private static native void outJsCallGwt() /*-{
  72. $wnd.outJsCallGwt = function (str) {
  73. alert(“此处是gwt:”+ str);
  74. };
  75. }-*/;
  76. }
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta http-equiv=”content-type” content=”text/html; charset=UTF-8″>
  5. <link type=”text/css” rel=”stylesheet” href=”TestCall.css” mce_href=”TestCall.css”>
  6. <title>Web Application Starter Project</title>
  7. <mce:script language=JavaScript><!–
  8. function callOutJs(str){
  9. alert(‘此处是外部js方法:’+ str);
  10. }
  11. // –></mce:script>
  12. <mce:script type=”text/javascript” language=”javascript” src=”testcall/testcall.nocache.js” mce_src=”testcall/testcall.nocache.js”></mce:script>
  13. </head>
  14. <body>
  15. <!– OPTIONAL: include this if you want history support –>
  16. <iframe src=”javascript:”” mce_src=”javascript:”” id=”__gwt_historyFrame” tabIndex=’-1′ style=”position:absolute;width:0;height:0;border:0″></iframe>
  17. <!– RECOMMENDED if your web app will not function without JavaScript enabled –>
  18. <noscript>
  19. <div style=”width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif”>
  20. Your web browser must have JavaScript enabled
  21. in order for this application to display correctly.
  22. </div>
  23. </noscript>
  24. <h1>Web Application Starter Project</h1>
  25. <table align=”center”>
  26. <tr>
  27. <td colspan=”2″ style=”font-weight:bold;” mce_style=”font-weight:bold;”>Please enter your name:</td>
  28. </tr>
  29. <tr>
  30. <button onclick=”outJsCallGwt(‘外部按钮被点击’)”>点击</button>
  31. <td id=”nameFieldContainer”></td>
  32. <td id=”sendButtonContainer”></td>
  33. </tr>
  34. <tr>
  35. <td colspan=”2″ style=”color:red;” mce_style=”color:red;” id=”errorLabelContainer”></td>
  36. </tr>
  37. </table>
  38. </body>
  39. </html>

备注: 以上html代码中<mce:script  应该为<script 由于csdn代码编辑器自动改变了值

  1. <script language=JavaScript>
  2. function callOutJs(str){
  3. alert(‘此处是外部js方法:’+ str);
  4. }
  5. <script>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896