creating a string for a link in grails without g:link - grails

In a tag like below:
<a data-url="/new/request/showText/${requestInstance.id}"> view text</a>
Is there a way to not hardcode the url like I have?
I tried using <g:link action="showText" id="${requestInstance.id}"> but that renders to an anchor tag.
What I'm doing might be find but I'm just curious if there is a better way to do this in grails?

You may use
${createLink(action:'myaction',params:[datasetId:dataset.id])}
for full control. It just returns something like http://myapp/myaction/123 and supportsall the params which g:link supports.
To be more specific:
<a data-url="${createLink(action:'showText',id: requestInstance.id)}"> view text</a>
should work.

You can use the createLink function inside of ${..}.
So in your case this would be:
<a data-url="${createLink(controller: 'yourController', action: 'yourAction', params:[param1: 'value1'])}"> view text</a>

Related

How to set shiro principal's property as thymeleaf tag attribute?

I have a demand that I want to use a shiro principal's property as thymeleaf tag's attribute.
I use the lib thymeleaf-extras-shiro, it's true to use like this.
<a>
<shiro:principal property='imgUrl'/>
</a>
but I want to set the property 'imgUrl' as tag '' attribute 'href'.
so anybody could tell me about this?
Thank you.
I haven't looked at thymeleaf-extras-shiro in a while but it might be something like:
<a href="${shiro.principal.imgUrl}"/>
// or maybe
<a href="${shiro.principal['imgUrl]'}"/>

Create custom GSP tag without writing Groovy code

Is it possible to create a custom GSP tag without writing Groovy code and embedding my HTML in the code (i.e. more of a JSP style way of creating a custom tag)?
I have a menu that consists of a bunch of items something like:
<li class="menu-item">
<g:link controller="someController" action="someAction" id="123">
My Item Text
</g:link>
</li>
I would like to create a new GSP tag to simplify my pages since it will be repeated multiple times. So, I'd like to create something like:
<my:menuitem controller="someController" action="someAction" id="123" text="My Item Text"/>
I know that I can create a custom taglib and create the tag using Groovy code. However I really don't like the idea of embedding HTML into a Groovy file. In the past I have created JSP taglibs in essentially a JSP file without writing Java code. So far looking at the documentation for Grails I haven't seen a similar style.
As a side note, can custom JSP tags be used within GSP?
You can do this with a template via the render tag, as explained in the "Views and Templates" section of the docs. Its worth noting you name the template file with a leading underscore but you refer to it in the render tag without the underscore.
The other alternative is to use a custom taglib as you described but to create your HTML with the Groovy MarkupBuilder. It takes a bit of getting used to (syntax is a bit strange) but once you've done a few times it becomes second nature.
The only way I can see to do what you want to do without a Taglib is to use g:render and pass in your values into the model attribute. Like this:
<g:render template="myTemplate" model="[controller: 'someController', action: 'someAction', id: 123, text: 'My Text Item']" />
Then in your actual template you will have the following:
<li class="menu-item">
<g:link controller="${controller}" action="${action}" id="${id}">
${text}
</g:link>
</li>

How can I show img inside anchor tag?

I want to do something like:
<img src="abc.jpg"/>
But when I see Html.ActionLink I can find any overload that allows me to do that. How can I achieve the above using Action.HtmlLink?
The easiest way is not to use Html.Action link and write plain old HTML.
<a href="<%=Url.Action("Action", "Controller")%>">
<img src="<%=Url.Content("~/Images/abc.jpg")%>" />
</a>
To achive the result your asking with ActionLink you've to write your own extension.
Look at this article

How to create link in asp.net mvc with html tags in title?

How can i create link using this - http://msdn.microsoft.com/en-us/library/dd505243.aspx
html.RouteLink using html tags in title?
In result i would see something like this :
<a class="" href="#"><div class="someclass">1</div><div class="someotherclass"></div></a>
Why not you try to use Url.Action() method.. Something like this :
<div class="someclass">1</div><div class="someotherclass"></div>
Only suggestion. Example is written using Razor syntax, but you can also simple use it with ASPX view engine...

Creating a controller / action href

While Grails has several tags to generate either resource links or anchor tags, what about a plain href? Say I'm coding some AJAX functions on my own and I want to say something like
$.post("<g:href controller='mycontroller' action='update' />');
I think createLink is what you're looking for:
<a href="${createLink(controller:'mycontroller', action:'update')}" />

Resources