Grails Groovy—如何识别文本中的url并将其显示为超链接
我尝试了encodeAsHTML(),如下所示:Grails Groovy—如何识别文本中的url并将其显示为超链接,grails,groovy,hyperlink,gsp,Grails,Groovy,Hyperlink,Gsp,我尝试了encodeAsHTML(),如下所示: <p class="common-textmb-30">${direction?.description?.encodeAsHTML()}</p> ${direction?.description?.encodeAsHTML()} 其中“direction?.description”是用户在某些输入中输入的文本 它没有检测到url。encodeAsHTML只是转义保留的HTML符号(例如 字符串文本=attrs.tex
<p class="common-textmb-30">${direction?.description?.encodeAsHTML()}</p>
${direction?.description?.encodeAsHTML()}
其中“direction?.description”是用户在某些输入中输入的文本
它没有检测到url。
encodeAsHTML
只是转义保留的HTML符号(例如
字符串文本=attrs.text
给出一个direction?的示例。说明@injecteer的可能重复项-例如“我们的新站点是”为什么你认为,它应该检测ilnks?@injecteer-它应该在groovy@Deigot-如何将我在Grails中编写的这个class.p.s导入tagLib。我应该编写import java.net.URL吗?@Deigot-我遇到一个错误-没有方法的签名:com.threebaysover.EventTagLib.isUrl()适用于参数类型:(java.lang.String)值:[有一个输入错误。isUrl(text)
应该是isUrl(text)
。我不知道EventTagLib.isUrl()来自哪里,因为示例标记库名为ViewFormatterTagLib。所以我假设您将其命名为其他名称。
boolean isURL(String someString) {
try {
new URL(someString)
} catch (MalformedURLException e) {
false
}
}
class ViewFormatterTagLib {
static namespace = 'viewFormatter'
def renderAsLinkIfPossible = { attrs ->
String text = attrs.text
out << (
isURL(text) ? "<a href='${text}'>${text}</a>" : text
)
}
private boolean isURL(String someString) {
try {
new URL(someString)
} catch (MalformedURLException e) {
false
}
}
}
<p class="common-textmb-30">
<viewFormatter:renderAsLinkIfPossible text="${direction?.description"/>
</p>