Quantcast

Generating html in controller

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Generating html in controller

bhushan154
I will greatly appreciate any help with this.

I am trying to create a html div in a controller to render on the gsp. I am having issues with links when i try to add a href with <a href="<g:createLink controller='example' action='example />"Link

The rendered content tries to access a link with unwanted special characters.

Below is my code and output
def generateTableExample(template){
        def table = new StringBuilder()  
        table.append(""" <a href="<g:createLink controller='example' />">Link """)
    }

In the gsp, the link tries to access
(MYBASEURL)/template/viewTemplate/%3Cg:createLink%20controller='example'%20/%3E

Can someone please help me out with what I am missing here? How do i create a html div that includes a href with <g:createLink> somewhere in it.

Thank you
Bhushan
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

tomas lin
You can use the groovy version of the tag,

table.append( """ <a href="${ createLink( controller: 'example',
action: 'example' ) }" > Link </a>  """)


On Sat, Feb 25, 2012 at 4:06 AM, bhushan154 <[hidden email]> wrote:

> I will greatly appreciate any help with this.
>
> I am trying to create a html div in a controller to render on the gsp. I am
> having issues with links when i try to add a href with <a
> href=&quot;&lt;g:createLink controller='example' action='example />"Link
>
> The rendered content tries to access a link with unwanted special
> characters.
>
> Below is my code and output
> def generateTableExample(template){
>        def table = new StringBuilder()
>        table.append(""" <a href=&quot;&lt;g:createLink controller='example'
> />">Link  """)
>    }
>
> In the gsp, the link tries to access
> (MYBASEURL)/template/viewTemplate/%3Cg:createLink%20controller='example'%20/%3E
>
> Can someone please help me out with what I am missing here? How do i create
> a html div that includes a href with <g:createLink> somewhere in it.
>
> Thank you
> Bhushan
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Generating-html-in-controller-tp4419373p4419373.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

Jonathan Andrew Ong
How about rendering another template instead? You won't have any trouble rendering your links/anchors.
render template:'templateName',model:[aDomainClass:DomainClass]


On Sat, Feb 25, 2012 at 7:07 PM, Tomas Lin <[hidden email]> wrote:
You can use the groovy version of the tag,

table.append( """ <a href="${ createLink( controller: 'example',
action: 'example' ) }" > Link </a>  """)


On Sat, Feb 25, 2012 at 4:06 AM, bhushan154 <[hidden email]> wrote:
> I will greatly appreciate any help with this.
>
> I am trying to create a html div in a controller to render on the gsp. I am
> having issues with links when i try to add a href with <a
> href=&quot;&lt;g:createLink controller='example' action='example />"Link
>
> The rendered content tries to access a link with unwanted special
> characters.
>
> Below is my code and output
> def generateTableExample(template){
>        def table = new StringBuilder()
>        table.append(""" <a href=&quot;&lt;g:createLink controller='example'
> />">Link  """)
>    }
>
> In the gsp, the link tries to access
> (MYBASEURL)/template/viewTemplate/%3Cg:createLink%20controller='example'%20/%3E
>
> Can someone please help me out with what I am missing here? How do i create
> a html div that includes a href with <g:createLink> somewhere in it.
>
> Thank you
> Bhushan
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Generating-html-in-controller-tp4419373p4419373.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

bhushan154
In reply to this post by tomas lin
I get this error message when I try what you suggested

Class:groovy.lang.MissingMethodException
Message:No signature of method: com.example.worksheets.TableGeneratorService.createLink() is applicable for argument types: (java.util.LinkedHashMap) values: [[controller:home]]

Code:
table.append(""" <a href="${createLink( controller: 'home' ) } " > Home   """)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

bhushan154
In reply to this post by Jonathan Andrew Ong
@Jonathan Andrew Ong
The problem is, I have various types of test objects in a worksheet. and depending on the type of test I need to generate different links. It is much easier to do this in a controller rather than a template.

But I am still unable to create links in a controller and pass it to the view. I tried what Tomas Lin suggested:
table.append(""" <a href="${createLink( controller: 'home' ) } " > Home   """)

But I get the following error:
Class:groovy.lang.MissingMethodException
Message:No signature of method: com.example.worksheets.TableGeneratorService.createLink() is applicable for argument types: (java.util.LinkedHashMap) values: [[controller:home]]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

Jason Davis
def linkMap = generateLinks()
render(template:'linksTemplate' , model:linkMap)

On Sat, Feb 25, 2012 at 9:33 AM, bhushan154 <[hidden email]> wrote:

> @Jonathan Andrew Ong
> The problem is, I have various types of test objects in a worksheet. and
> depending on the type of test I need to generate different links. It is much
> easier to do this in a controller rather than a template.
>
> But I am still unable to create links in a controller and pass it to the
> view. I tried what Tomas Lin suggested:
> table.append("""  ${createLink( controller: 'home' ) }   Home    """)
>
> But I get the following error:
> Class:groovy.lang.MissingMethodException
> Message:No signature of method:
> com.example.worksheets.TableGeneratorService.createLink() is applicable for
> argument types: (java.util.LinkedHashMap) values: [[controller:home]]
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Generating-html-in-controller-tp4419373p4420430.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

bhushan154
I am doing exactly the same thing

def linkMap = generateLinks()
render(template:'linksTemplate' , model:linkMap)

So in generateLinks() method, I create a String (which in your example is the linkMap)

But the problem is, <a href="<g:createLink controller='home'  />">Link 
When rendered on the view will create a link to
(MYBASEURL)/template/viewTemplate/%3Cg:createLink%20controller='home'%20/%3E

Which is not what I want. I want it to link to the home controller.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

Matthias Hryniszak
First of all you're mixing responsibilities: 

the controller is responsible for controlling how the input data gets transformed into the output _data_
the view is responsible for turning that _data_ into some representation

So my advice to you is use a template like this

_link.gsp:
<a href="${createLink(controller: ctrl, action: act)">${linkText}</a>

like this in your controller:

render template: 'link', model: [ ctrl: 'example', act: 'example', linkText: 'Link' ]

HTH

Matthias
2012/2/25 bhushan154 <[hidden email]>
I am doing exactly the same thing

def linkMap = generateLinks()
render(template:'linksTemplate' , model:linkMap)

So in generateLinks() method, I create a String (which in your example is
the linkMap)

But the problem is, <a href=&quot;&lt;g:createLink controller='home'
/>">Link
When rendered on the view will create a link to
(MYBASEURL)/template/viewTemplate/%3Cg:createLink%20controller='home'%20/%3E

Which is not what I want. I want it to link to the home controller.


--
View this message in context: http://grails.1312388.n4.nabble.com/Generating-html-in-controller-tp4419373p4420636.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

Jason Davis
In reply to this post by bhushan154
thats not exactly what i meant


def genLinks = {
 [
   FOO_LINK: g.createLink(controller:'foo', action:'getFoo'),
  BAR_LINK: g.createLink(controller:'bar', action:'getBAR')
 ]
}


render(template:'links' , model:getlinks())


then in template


<div>
${FOO_LINK}
${BAR_LINK}
</div>






On Sat, Feb 25, 2012 at 11:22 AM, bhushan154 <[hidden email]> wrote:

> I am doing exactly the same thing
>
> def linkMap = generateLinks()
> render(template:'linksTemplate' , model:linkMap)
>
> So in generateLinks() method, I create a String (which in your example is
> the linkMap)
>
> But the problem is, <a href=&quot;&lt;g:createLink controller='home'
> />">Link
> When rendered on the view will create a link to
> (MYBASEURL)/template/viewTemplate/%3Cg:createLink%20controller='home'%20/%3E
>
> Which is not what I want. I want it to link to the home controller.
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Generating-html-in-controller-tp4419373p4420636.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

Matthias Hryniszak
Well.. in that case you should do that in the template directly

[ ctrl1: 'example1', act1: 'example1', linkText1: 'Link 1', ctrl2: 'example2', act2: 'example2', linkText1: 'Link 2' ]

<div>
  <a href="${createLink(controller: ctrl1, action: act1)">${linkText1}</a> 
  <a href="${createLink(controller: ctrl1, action: act1)">${linkText1}</a> 
</div>

or use the g:each tag to render a list of links:

[ links: [ [ c: 'example1', a: 'example1', t: 'Link1' ], [ c: 'example2', a: 'example2', t: 'Link2' ] ] ]

<div>
  <g:each var="link" in="${links}">
    <a href="${createLink(controller: link.c, action: link.a)}">${link.t}</a>
  </g:each>
</div>

This way you can retrieve the list of data to create links from, let's say, database or some other external source and still have the capability to decompose the presentation from retrieval logic.

Either way don't mix the responsibilities - it leads to bad design all along.

Matthias

2012/2/25 Jason Davis <[hidden email]>
thats not exactly what i meant


def genLinks = {
 [
  FOO_LINK: g.createLink(controller:'foo', action:'getFoo'),
 BAR_LINK: g.createLink(controller:'bar', action:'getBAR')
 ]
}


render(template:'links' , model:getlinks())


then in template


<div>
${FOO_LINK}
${BAR_LINK}
</div>






On Sat, Feb 25, 2012 at 11:22 AM, bhushan154 <[hidden email]> wrote:
> I am doing exactly the same thing
>
> def linkMap = generateLinks()
> render(template:'linksTemplate' , model:linkMap)
>
> So in generateLinks() method, I create a String (which in your example is
> the linkMap)
>
> But the problem is, <a href=&quot;&lt;g:createLink controller='home'
> />">Link
> When rendered on the view will create a link to
> (MYBASEURL)/template/viewTemplate/%3Cg:createLink%20controller='home'%20/%3E
>
> Which is not what I want. I want it to link to the home controller.
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Generating-html-in-controller-tp4419373p4420636.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

bhushan154
In reply to this post by Matthias Hryniszak
Thanks everyone.

Matthias thank you for your clarification.

So what I did was get the baseUrl by
String path = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()

And in the controller
table.append("<a href='" + path +"/home'>my link ")

This works.

Jason the issue is, I will have several links, and each link might have a different id of the the object. So i figured the easiest way is to generate a table from a controller or a service and pass it to the view to render.

Now I am left with three other questions:
1. What is the best way to update several objects from a single page
2. Can I retrieve the baseUrl in any other way then how I have done it above

Thank you all for your replies
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

tomas lin
In reply to this post by bhushan154
You're trying to call a taglib action within a service, which
obviously won't work.

Use the link generator if doing this outside a controller -
http://mrhaki.blogspot.com/2012/01/grails-goodness-generate-links-outside.html

On Sat, Feb 25, 2012 at 4:29 PM, bhushan154 <[hidden email]> wrote:

> I get this error message when I try what you suggested
>
> Class:groovy.lang.MissingMethodException
> Message:No signature of method:
> com.example.worksheets.TableGeneratorService.createLink() is applicable for
> argument types: (java.util.LinkedHashMap) values: [[controller:home]]
>
> Code:
> table.append("""  ${createLink( controller: 'home' ) }   Home    """)
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Generating-html-in-controller-tp4419373p4420417.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

Jason Davis
In reply to this post by bhushan154
supplying a path to a template to use as a link is not mixing
anything. Its not like the entire <a> was generated. Just the string
"/bar/getBar".

bhushan154 anything that can be calculated in the view can be done in
a controller or service too.  You might clarify what you are doing.
Why not use relative linking btw?
Maybe im missing something

good luck!



On Sat, Feb 25, 2012 at 11:59 AM, bhushan154 <[hidden email]> wrote:

> Thanks everyone.
>
> Matthias thank you for your clarification.
>
> So what I did was get the baseUrl by
> String path = "http://" + request.getServerName() + ":" +
> request.getServerPort() + request.getContextPath()
>
> And in the controller
> table.append(" " + path  +"/home my link  ")
>
> This works.
>
> Jason the issue is, I will have several links, and each link might have a
> different id of the the object. So i figured the easiest way is to generate
> a table from a controller or a service and pass it to the view to render.
>
> Now I am left with three other questions:
> 1. What is the best way to update several objects from a single page
> 2. Can I retrieve the baseUrl in any other way then how I have done it above
>
> Thank you all for your replies
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Generating-html-in-controller-tp4419373p4420718.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

Jason Davis
In reply to this post by tomas lin
you can call them in a controller no problem. this is inside a service?

On Sat, Feb 25, 2012 at 12:06 PM, Tomas Lin <[hidden email]> wrote:

> You're trying to call a taglib action within a service, which
> obviously won't work.
>
> Use the link generator if doing this outside a controller -
> http://mrhaki.blogspot.com/2012/01/grails-goodness-generate-links-outside.html
>
> On Sat, Feb 25, 2012 at 4:29 PM, bhushan154 <[hidden email]> wrote:
>> I get this error message when I try what you suggested
>>
>> Class:groovy.lang.MissingMethodException
>> Message:No signature of method:
>> com.example.worksheets.TableGeneratorService.createLink() is applicable for
>> argument types: (java.util.LinkedHashMap) values: [[controller:home]]
>>
>> Code:
>> table.append("""  ${createLink( controller: 'home' ) }   Home    """)
>>
>> --
>> View this message in context: http://grails.1312388.n4.nabble.com/Generating-html-in-controller-tp4419373p4420417.html
>> Sent from the Grails - user mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

Matthias Hryniszak
In reply to this post by tomas lin
String path = "http://" + request.getServerName() + ":" +
request.getServerPort() + request.getContextPath()

===

def path = "http://${request.serverName}:${request.serverPort}${request.contextPath}"

Matthias

2012/2/25 Tomas Lin <[hidden email]>
You're trying to call a taglib action within a service, which
obviously won't work.

Use the link generator if doing this outside a controller -
http://mrhaki.blogspot.com/2012/01/grails-goodness-generate-links-outside.html

On Sat, Feb 25, 2012 at 4:29 PM, bhushan154 <[hidden email]> wrote:
> I get this error message when I try what you suggested
>
> Class:groovy.lang.MissingMethodException
> Message:No signature of method:
> com.example.worksheets.TableGeneratorService.createLink() is applicable for
> argument types: (java.util.LinkedHashMap) values: [[controller:home]]
>
> Code:
> table.append("""  ${createLink( controller: 'home' ) }   Home    """)
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Generating-html-in-controller-tp4419373p4420417.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

bhushan154
In reply to this post by tomas lin
Tomas, the link what you gave does exactly what I wanted.

Thanks a lot

--
View this message in context: http://grails.1312388.n4.nabble.com/Generating-html-in-controller-tp4419373p4420974.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating html in controller

bhushan154
In reply to this post by tomas lin
Tomas, the link what you gave does exactly what I wanted.

Loading...