使DWR传递空值,而不是转换为false

时间:2022-10-02 23:45:27

I am working with DWR inside the context of a Spring 3.x Web MVC application, where my dwr-beans.xml file declares a bean like this:

我在Spring 3.x Web MVC应用程序的上下文中使用DWR,其中我的dwr-beans.xml文件声明了这样的bean:

<dwr:configuration>
    <dwr:convert type="bean" class="com.mypackage.Customer"/>
</dwr:configuration>

The com.mypackage.Customer class has one attribute of type Boolean (the object wrapper, not a boolean primitive).

com.mypackage.Customer类有一个Boolean类型的属性(对象包装器,而不是布尔基元)。

This attribute has three different states that are meaningful to the business logic. It can be true or false, obviously... but a null value is meaningful for signaling that a selection hasn't be made yet.

此属性具有三种对业务逻辑有意义的不同状态。它可以是真或假,显然......但是空值对于发信号通知还没有进行选择是有意义的。

Unfortunately, when a Java object is passed across to JavaScript through a DWR AJAX call... a null value shows up as false on the JavaScript object. I'm losing that third meaningful "neither of the above" state.

不幸的是,当Java对象通过DWR AJAX调用传递给JavaScript时...空值在JavaScript对象上显示为false。我失去了第三个有意义的“上述两个”状态。

Google searching has not been very fruitful, unfortunately. Does anyone know if there is a way to make DWR properly pass across a Java null as a JavaScript null (or undefined)? Or might I be doing something wrong in the first place?

不幸的是,谷歌搜索并不是很有成效。有没有人知道是否有办法让DWR正确地通过Java null作为JavaScript null(或未定义)?或者我可能首先做错了什么?

2 个解决方案

#1


0  

This is a piece of code, that can be found in PrimitiveConverter of DWR source code

这是一段代码,可以在DWR源代码的PrimitiveConverter中找到

if (paramType == Boolean.class)
{
    if (trimValue.length() == 0)
    {
        return null;
    }

    return (T) Boolean.valueOf(trimValue);
}

Whenever you pass null, asd or anything else you've get null as result. You can try to extend DWR with your own implementation, it could be rather tricky. Or use String instead in your bean with 3 state ("true", "false", "null").

每当你传递null,asd或其他任何东西时你都会得到null作为结果。您可以尝试使用自己的实现扩展DWR,这可能相当棘手。或者在你的bean中使用String,状态为3(“true”,“false”,“null”)。

#2


0  

It turns out that DWR does handle null values correctly by default. My issue was further up the stack... not between the browser and the app server, but rather between the app server and the remote web service that it was calling for the data.

事实证明,DWR默认情况下会正确处理空值。我的问题是进一步上升...不是在浏览器和应用服务器之间,而是在应用服务器和它为数据调用的远程Web服务之间。

#1


0  

This is a piece of code, that can be found in PrimitiveConverter of DWR source code

这是一段代码,可以在DWR源代码的PrimitiveConverter中找到

if (paramType == Boolean.class)
{
    if (trimValue.length() == 0)
    {
        return null;
    }

    return (T) Boolean.valueOf(trimValue);
}

Whenever you pass null, asd or anything else you've get null as result. You can try to extend DWR with your own implementation, it could be rather tricky. Or use String instead in your bean with 3 state ("true", "false", "null").

每当你传递null,asd或其他任何东西时你都会得到null作为结果。您可以尝试使用自己的实现扩展DWR,这可能相当棘手。或者在你的bean中使用String,状态为3(“true”,“false”,“null”)。

#2


0  

It turns out that DWR does handle null values correctly by default. My issue was further up the stack... not between the browser and the app server, but rather between the app server and the remote web service that it was calling for the data.

事实证明,DWR默认情况下会正确处理空值。我的问题是进一步上升...不是在浏览器和应用服务器之间,而是在应用服务器和它为数据调用的远程Web服务之间。