简体   繁体   中英

Grails - Referencing Absolute URL in gsp

In my index.gsp for one of the column values for a table I provide the following:

<td><g:link action = "redirect(url: 'http://www.google.com')">www.google.com</g:link></td>

However, the link that shows on the page is ->

http://localhost:8080/APP_NAME/VIEW_NAME/redirect(url: 'http://www.google.com')

What's the workaround to avoid the inclusion of the base url in the beginning. I want the link to be the absolute URL ->

http://www.google.com

Based on some of the comments below, the following works ->

<td><a href='http://www.google.com'>www.google.com</a></td>

However, when I reference the field of a bean that I wish to display like this ->

<td><a href=${fieldValue(bean: testRunInstance, field: "artifactLink")}>${fieldValue(bean: testRunInstance, field: "artifactLink")}</a></td>

the link displays correct (www.google.com), while the actual link resolves to ->

http://localhost:8080/APP_NAME/www.google.com

How do I eliminate the reference to the below Base URL?

http://localhost:8080/APP_NAME/

Use the tag anchor of the standard HTML

<a href='http://www.google.com'>www.google.com</a>

EDIT

You can change:

<td><a href=${fieldValue(bean: testRunInstance, field: "artifactLink")}>${fieldValue(bean: testRunInstance, field: "artifactLink")}</a></td>

By:

<td>
    <a href="http://${testRunInstance.artifactLink}">
        ${fieldValue(bean: testRunInstance, field: "artifactLink")}
    </a>
</td>

或者,如果您想要<g:link>

<g:link url="http://www.google.com">www.google.com</g:link>

我认为你可以使用

     <g:link url="http://www.google.com">www.google.com</g:link>
// Example, where artifactLink has a value, ddg.gg
<g:link base="http://" uri="${bean.artifactLink}">${bean.artifactLink}</g:link>
// Generates
<a href="http://ddg.gg">ddg.gg</a>


// Example, where artifactLink has a value, http://ddg.gg
<g:link base="http://" uri="${bean.artifactLink}">${bean.artifactLink}</g:link>
// Generates
<a href="http://ddg.gg">http://ddg.gg</a>


// Example, where artifactLink has a value, https://ddg.gg
<g:link base="http://" uri="${bean.artifactLink}">${bean.artifactLink}</g:link>
// Generates
<a href="https://ddg.gg">https://ddg.gg</a>

It ignores the base attribute altogether, when encounter a protocol in front of the uri value. Notice, it doesn't repeat http:// in the 2nd example, and used https:// instead, in the last one.

Moreover, you can specify target="_blank" , so page loads in a new tab or window.

NB: Should work for Grails 3.2 or >.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM