GWT JSNI 布尔值
这是我的代码:
package com.eggproject_hu.WPECommerceAdminSales.client;
import java.lang.Boolean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
public class AblakVillogo
{
public static Boolean focusedWindow = true;
private static Boolean init = false;
public static void setFocused(Boolean focus)
{
focusedWindow = focus;
}
public static Boolean getFocused()
{
return focusedWindow;
}
public static void focusVizsgalat()
{
if(focusedWindow == true)
{
GWT.log("igen");
}
else
{
GWT.log("nem");
}
}
public static void init()
{
if(init == false)
{
_init();
}
}
private native static void _init() /*-{
$wnd.jQuery(document).ready(function()
{
$wnd.jQuery($wnd).focus(function()
{
@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()(); @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(true);
$wnd.console.log("focus");
}).blur(function()
{
var ret = false;
@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()();
@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(false);
$wnd.console.log("blur");
});
});
}-*/;
}
我在浏览器控制台中看到了这一点:
未捕获的异常:java.lang.IllegalArgumentException:调用参数:布尔类型的 JS 值,预期为 java.lang.Boolean
我已经在Chrome和Firefox中进行了测试。
问题是什么?
感谢您的帮助!
Here's my code:
package com.eggproject_hu.WPECommerceAdminSales.client;
import java.lang.Boolean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
public class AblakVillogo
{
public static Boolean focusedWindow = true;
private static Boolean init = false;
public static void setFocused(Boolean focus)
{
focusedWindow = focus;
}
public static Boolean getFocused()
{
return focusedWindow;
}
public static void focusVizsgalat()
{
if(focusedWindow == true)
{
GWT.log("igen");
}
else
{
GWT.log("nem");
}
}
public static void init()
{
if(init == false)
{
_init();
}
}
private native static void _init() /*-{
$wnd.jQuery(document).ready(function()
{
$wnd.jQuery($wnd).focus(function()
{
@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()(); @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(true);
$wnd.console.log("focus");
}).blur(function()
{
var ret = false;
@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()();
@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(false);
$wnd.console.log("blur");
});
});
}-*/;
}
I see this in the browser console:
uncaught exception: java.lang.IllegalArgumentException: invoke arguments: JS value of type boolean, expected java.lang.Boolean
I've tested in Chrome and Firefox.
What is the problem?
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将布尔值声明为原始布尔值才能从 javascript 设置值
,并且不需要在调用中指定 L/java/lang/Boolean,而是指定 Z
You have to declare the Boolean as the primitive boolean to set the value from javascript
and you dont need to specifiy L/java/lang/Boolean in the call but Z instead
要么遵循 Daniel 的建议,但随后您必须更改方法以采用
boolean
参数(即始终使用boolean
),或者您可以显式强制转换/装箱您的JSNI 方法中 java.lang.Boolean 中的 boolean:...即使在您的情况下,因为该值是常量,我宁愿直接使用
Boolean
常量TRUE
和FALSE
:话虽如此,我确实相信 Daniel 的建议最适合您。
Either follow Daniel's advice, but then you have to change your method to take a
boolean
argument (i.e. useboolean
all the way through), or you can explicitly cast/box your boolean in a java.lang.Boolean in your JSNI method:…even though in your case, because the value is a constant, I'd rather directly use the
Boolean
constantsTRUE
andFALSE
:That being said, I do believe Daniel's advice is your best fit.