HTML&GRAILS - urls text is not displayed as hyperlink

戏子无情 提交于 2019-12-12 03:19:26

问题


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>


回答1:


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.



来源:https://stackoverflow.com/questions/32370492/htmlgrails-urls-text-is-not-displayed-as-hyperlink

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!