ColdFusion Java 未找到方法异常

ColdFusion Java Method Not Found Exception

这个问题一直让我抓狂。我试图在 ColdFusion 中使用 Java 来创建一个 Document 对象。当我这样做时:

nd = createObject("java","javax.xml.parsers.DocumentBuilder");

我可以转储 nd 并看到它正确加载了所有方法:

object of javax.xml.parsers.DocumentBuilder Class Name javax.xml.parsers.DocumentBuilder 

Method / Return Type

getDOMImplementation() / org.w3c.dom.DOMImplementation

getSchema() / javax.xml.validation.Schema 

isNamespaceAware() / boolean 

isValidating() / boolean 

isXIncludeAware() / boolean 

newDocument() / org.w3c.dom.Document 

parse(java.io.File) / org.w3c.dom.Document 

parse(java.lang.String) / org.w3c.dom.Document 

parse(org.xml.sax.InputSource) / org.w3c.dom.Document 

parse(java.io.InputStream, java.lang.String) / org.w3c.dom.Document 

parse(java.io.InputStream) / org.w3c.dom.Document 

reset() / void 

setEntityResolver(org.xml.sax.EntityResolver) / void 

setErrorHandler(org.xml.sax.ErrorHandler) / void 

我正在尝试调用 newDocument() 方法。我在 cfscript 和 cfsets 中都尝试了以下所有操作:

nd.newDocument();
nd.newDocument(JavaCast("null",""));
nd = createObject("java","javax.xml.parsers.DocumentBuilder").newDocument();
nd = createObject("java","javax.xml.parsers.DocumentBuilder").newDocument(JavaCast("null",""));

但是,无论我尝试什么方法,我都会得到这个错误:

Either there are no methods with the specified method name and argument types or the isNamespaceAware method is overloaded with argument types that ColdFusion cannot decipher reliably. ColdFusion found 0 methods that match the > provided arguments. If this is a Java object and you verified that the method > exists, use the javacast function to reduce ambiguity.

我可以看到该方法已加载。该方法没有被重载。它不需要任何参数。而且,即使我明确告诉 CF 我正在传递 null,它也不能'找不到方法..

我尝试访问 class 中的其他方法 - 但它也找不到那些..我不确定为什么我可以转储 class 的内容 - 我可以看到所有的方法..但是,当我尝试调用它们时,CF 不知何故变得混乱并且找不到它们..

任何想法都会非常有帮助..

谢谢!!

您必须为 documentBuilder 工厂创建一个对象。在工厂的帮助下,您可以获得真实的 xml 信息。在这里,我创建了对象并使用 documentbuilderfactory 调用了 parse 方法。 您还必须注入一个 newInstance() 然后只有您可以访问 newdocument() 方法。 我的Xml内容:testParse.xml

<?xml version="1.0"?>
<company>
<staff id="1001">
    <firstname>yong</firstname>
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <salary>100000</salary>
</staff>
<staff id="2001">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
</staff>
</company>

*CF码:

<cfset myObj = createObject("java","javax.xml.parsers.DocumentBuilderFactory")>
<cfset createDocs = myObj.newInstance().newDocumentBuilder()>
<cfset parseDocs = createDocs.parse(expandpath('/testParse.xml'))>
<cfset getNodeName = parseDocs.getDocumentElement().getNodeName()>
<cfset getList = parseDocs.getElementsByTagName("staff")>

<cfloop index="i" from="1" to="#getList.getlength()#">
     <!--- Do your business logic here  --->
</cfloop>

希望对您有所帮助。谢谢。

我在 java class 上从 ColdFusion 调用方法时遇到了同样的错误。我正在尝试使用 setPropertyName 方法,如下所示。

    <cfobject action="create" type="java" name="This.txnRequest" class="#AnetAPI#.TransactionRequestType" />
    <cfset authTxnType=CreateObject("java", "#AnetAPI#.TransactionTypeEnum") />
    <cfset This.txnRequest.setTransactionType(authTxnType.AUTH_CAPTURE_TRANSACTION) />

事实证明,ColdFusion 希望您直接访问 EJB 中的属性。您还需要像这样显式调用 EJB 构造函数:

    <cfset This.txnRequest.init() />
    <cfset This.txnRequest.TransactionType=authTxnType.AUTH_CAPTURE_TRANSACTION />

CF 隐式调用 set 方法,如 Adobe Docs 中所述。不能直接调用。