GWT:调用外部JavaScript库(sjcl)方法 [英] GWT: Calling external JavaScript library (sjcl) method

查看:430
本文介绍了GWT:调用外部JavaScript库(sjcl)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的GWT项目中使用外部加密库(客户端)。 (特别是斯坦福JavaScript加密库, http://crypto.stanford.edu/sjcl/
我在我的项目的gwt配置文件(MyProj.gwt.xml)中添加了这些行

 < script src = libs / sjcl / sjcl.js/> 
< script src =libs / sjcl / core / sha256.js/>

并且在我的安全类中创建了一个新方法:

  public static native String sha256(String ptext)
/ * - {
return sjcl.hash.sha256.hash(ptext);
} - * /;

但是当我把它叫做

 字符串结果= Security.sha256(password); 

我得到一个错误(sjcl is not defined )如果有人知道这些问题和javasript可以读取图书馆并告诉我正确的方法,那么我将节省大量的时间和精力 - 不要在怪异的JavaScript中进行挖掘。

UPD:

第一件事我明白了,外部库必须位于/ war目录中
其次,我无法使GWT如果我只在gwt.xml文件中添加以下内容,请导入* .js文件

 < script src =libs / sjcl /sjcl.js\"></script> 
< script src =libs / sjcl / core / sha256.js>< / script>

我必须为入口点的html源代码添加类似的行

 < script src =/ sjcl / sjcl.js>< / script> 
< script src =/ sjcl / core / sha256.js>< / script>

通过这种方式导入,我可以使用Chrome Inspector进行检查



我试图使用另一个库,不像sjcl那么复杂,而且工作正常。
显然,我没有使用sjcl。
另一个库更简单,只是纯粹的方法,sjcl使用类和东西,但因为我根本不知道JS,所以我无法弄清楚如何在不挖掘JS语法的情况下正确调用这些东西。我真的很想跳过这部分。



任何人都可以查看sjcl代码并告诉我如何正确调用该方法吗?我认为,源代码非常小,JS Pro可以一目了然。



UPD 2:
事实证明,将以下代码添加到gwt.xml就足够了,只需将库放在/ war中,并注意路径中前面的/。如果你不把它放在那里,GWT会在模块目录

中寻找你的lib lib
$ b

UPD 3:
不要忘记清除浏览器的缓存或找不到错误不会消失

解决方案

确保当你说话时关于在全局上下文中以 $ wnd 为前缀的对象,即应用程序正在运行的主窗口实例这在JSNI文档中有记录,请参阅 https://开发人员。 google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI#writing

  public static native String sha256( String ptext)
/ * - {
return $ wnd.sjcl.hash.sha256.hash(ptext);
} - * /;


I want to use an external crypto library in my GWT project (client side). (particularly, Stanford Javascript Crypto Library, http://crypto.stanford.edu/sjcl/ I have added these lines in my project's gwt configuration file (MyProj.gwt.xml)

<script src="libs/sjcl/sjcl.js" />
<script src="libs/sjcl/core/sha256.js" />

And have created a new method in my security class:

public static native String sha256(String ptext)
      /*-{
       return sjcl.hash.sha256.hash(ptext);
       }-*/;

But when I call it

String result = Security.sha256("password");

I get an error ("sjcl is not defined")

If someone who knows these issues and javasript could read the library and tell me the right way, I will be saving much time and effort - no digging in monstrous JavaScript.

UPD:

First thing I figured out, external library must be in /war directory Second, I couldn't make GWT import the *.js files if I added the following just to gwt.xml files

<script src="libs/sjcl/sjcl.js"></script>
<script src="libs/sjcl/core/sha256.js"></script>

I had to add similar lines to Entry point's html source

<script src="/sjcl/sjcl.js"></script>
<script src="/sjcl/core/sha256.js"></script>

This way import works, I could check it with Chrome Inspector

I tried to use another library, not so complicated as sjcl, and it worked fine. So, obviously, I'm not using sjcl right. Another library is simpler, just pure methods, sjcl uses classes and stuff, but as I don't know JS at all, I can't figure out how to call such things properly without digging into JS syntax. I really would like to skip this part.

Can anybody look into the sjcl code and tell me how to call the method properly? The source is really small and JS pro can figure it out at a glance, I think.

UPD 2: It turned out that adding the following code to gwt.xml is enough, just put library in /war and pay attention to the preceding "/" in the path. If you don't put it there, GWT will be seeking for you lib in module directory

UPD 3: And don't forget to clear browser's cache or "not found" error won't go away

解决方案

Make sure that when you talk about objects in the global context that you prefix them with $wnd, meaning the main window instance that the app is running in. This is documented in the JSNI docs, see https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI#writing.

public static native String sha256(String ptext)
  /*-{
      return $wnd.sjcl.hash.sha256.hash(ptext);
  }-*/;

这篇关于GWT:调用外部JavaScript库(sjcl)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆