HTML&GRAILS - urls text is not displayed as hyperl

2019-09-09 14:39发布

I have the following gsp code:

<p class="common-text ">${direction?.description?.toHtml()?.decodeHTML()}</p>

direction?.description is a text which the user entered in some input field. This text could includes website URLs like Visit my new site www.example.com

How can I verified if some of the text contains a URL, and if so display it as hyperlink?

The text above should be transformed to something like:

Visit my new site <a href="www.example.com"> www.example.com </a>

标签: html5 grails gsp
1条回答
爷、活的狠高调
2楼-- · 2019-09-09 15:04

You have two way to do this.

Server side with a Grails taglib

You could create a custom Grails taglib that look for URLs in your text and replace with <a> tags when needed. It could be used like this:

<myTagLib:replaceUrls>${direction?.description}</myTagLib:replaceUrls>

Client side with javascript

You can use a javascript library to search URLs in strings and then auto replace them with the proper hyperlink. See this question for more information about this techinque.

查看更多
登录 后发表回答