将 Java 对象数组返回到 Coldfusion 组件
在 Coldfusion 中创建 Java 对象相当简单:
variable = CreateObject("JAVA", "java.object").init(JavaCast("primitiveType", cfVar));
但是,如果 Java 方法返回 Java 对象列表,则使用 Java 返回类型就没那么简单了:
newVariable = variable.returnJavaObjectCollection();
是否有最佳实践? > 用于处理以数组或 ArrayCollection 形式返回的 Java 对象?
It is fairly straightforward to create Java objects in Coldfusion:
variable = CreateObject("JAVA", "java.object").init(JavaCast("primitiveType", cfVar));
However, it isn't as straightforward to work with Java return types if, say, a Java method returns a list of Java objects:
newVariable = variable.returnJavaObjectCollection();
Is there a best practice for working with Java objects that are returned in an array or an ArrayCollection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我认为我正确理解了你的问题(抱歉,你不是 100% 清楚) - 你应该能够在 ColdFusion 中调用 Java 对象的方法,就像在 Java 中通常调用的方法一样。
例如,您可以调用 java.lang 上的方法。 String 对象,在 ColdFusion 中处理 String 时。
所以没有什么问题:
如果您正在处理 java.util.List 的实例,您会发现 9/10 次,ColdFusion 的本机数组函数可以很好地工作 - 因为 ColdFusion 数组实际上是 java.util 的实现.列表也是如此。
所以应该仍然有效,ArrayAppend()、ArrayContains() 等都应该按预期工作。
如果失败,您仍然可以访问 List 的底层 API:
http://download.oracle.com/javase/6 /docs/api/java/util/List.html
但要记住的是,一切都从 0 开始,而不是索引 1。
因此,要获取 Java 列表中的第一个值,应该是
:比CF方式:
否则,这就是它的真正意义。
您可能应该注意到,直接调用 Java 方法的开销很小,因为它是使用 Reflection,因此,如果您可以使用原生 CF 函数,那通常会更好,但除了原生交互之外,您没有其他办法。
有关详细信息,您可能需要尝试ColdFusion Java 文档
If I think I understand your question correctly (sorry, you aren't 100% clear) - you should be able to call methods on Java objects in ColdFusion, much as you would normally within Java.
For example, you can call methods on a java.lang.String object, when dealing with a String in ColdFusion.
So there is nothing wrong with:
If you are dealing with an instance of java.util.List, you will find that 9/10 times, ColdFusion's native array functions will work just fine with - as ColdFusion Arrays are actually implementations of java.util.List as well.
So <cfloop array="#foo#" ...> should still work, ArrayAppend(), ArrayContains(), etc should all work as expected.
Failing that, you still have access to the underlying API for List:
http://download.oracle.com/javase/6/docs/api/java/util/List.html
But the thing to remember, is that everything is from 0, rather than an index of 1.
So to get the first value in a Java List would be:
Rather than the CF way of:
Otherwise, that's really about it.
You should probably note that there is a small overhead on calling Java methods directly, as it is done on the fly using Reflection, so if you can use the native CF functions instead, that is usually better, but something you have no other recourse but to interact natively.
For more information you may want to try the ColdFusion Java Documentation
如果您返回一个
Vector
ColdFusion 中没有定义 null 对象,因此如果数组中的元素为 null,或者映射中的值为 null,或者您只是返回 null,则它们各自的变量将是未定义的。
请记住,您仍然可以在 ColdFusion 中调用复杂 java 对象上的 java 方法——包括除向量和映射之外的复杂集合上的方法。例如:
If your return a
Vector<Object>
it will work with ColdFusion's array utilities, and aHashMap<String,Object>
will work with ColdFusion's Struct utilities. A few notes though:null objects are not defined in ColdFusion, so if an element in the array is null, or a value in the map is null, or you just return null, their respective variables will be undefined.
Keep in mind that you can still call java methods on complex java objects in ColdFusion--including methods on sophisticated collections besides Vectors and Maps. For example: