問題描述
我正在嘗試使用 JSoup 和 ColdFusion 來清理一些 HTML,但遇到以下錯誤:
I am trying to use JSoup with ColdFusion to clean up some HTML but am encountering the following error:
找不到 addTags 方法.要么沒有具有指定方法名稱和參數類型的方法,要么 addTags 方法被 ColdFusion 無法可靠破譯的參數類型重載.ColdFusion 找到 0 個與提供的參數匹配的方法.如果這是一個 Java 對象并且您驗證了該方法存在,請使用 javacast 函數來減少歧義.
我的代碼如下:
<cfset jsoup = createObject('java','org.jsoup.Jsoup')>
<cfset Whitelist = createObject("java", "org.jsoup.safety.Whitelist")>
<cfset parsedhtml = jsoup.parse(form.contentrichtext)>
<cfset post = parsedhtml.body().html()>
<cfset post = jsoup.clean(post, Whitelist.none().addTags("span"))>
我已經轉儲了 Whitelist 對象,并且存在 add Tags 方法.如果我刪除 addTags() 方法并使用標準 JSoup 白名單之一,例如 basic()、none() 或 Relax(),則代碼運行完美.據我從其他在線示例中可以看出,這是使用 addTags() 方法的正確語法.
I have dumped out the Whitelist object and the add Tags method is present. If I remove the addTags() method and use one of the standard JSoup Whitelists such as basic(), none() or relaxed() then the code runs perfectly. As far as I can see from other online examples this is the correct syntax for using the addTags() method.
我對在 ColdFusion 中使用 Java 對象還很陌生,所以這讓我很困惑.
I am fairly new to using Java objects within ColdFusion so this has got me stumped.
任何幫助將不勝感激.
謝謝,邁克爾.
推薦答案
addTags
方法需要一個字符串數組,而不僅僅是一個字符串.先將值放入數組中:
The addTags
method expects an array of strings, not just a single string. Put the value into an array first:
<!--- create a CF array then cast it as type string[] --->
<cfset tagArray = javacast("string[]", ["span"]) >
<cfset post = jsoup.clean(post, Whitelist.none().addTags( tagArray ))>
據我從其他在線示例中可以看出,這是正確的語法
As far as I can see from other online examples this is the correct syntax
為了澄清,是正確的語法 - 對于java.在 java 中,您可以傳入 可變數量的參數 使用數組或以下語法:addTags("tag1", "tag2", ...)
.但是,CF 只支持數組語法.所以如果你cfdump這個java對象,你會在類名后面看到方括號,表示參數是一個數組:
To clarify, that is the correct syntax - for java. In java you can pass in a variable number of arguments using either an array or this syntax: addTags("tag1", "tag2", ...)
. However, CF only supports the array syntax. So if you cfdump the java object, you will see square brackets after the class name, which indicates the argument is an array:
method: addTags( java.lang.String[] ) // array of strings
這篇關于ColdFusion 和 JSoup - 找不到 addTags 方法錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!