Search code examples
grailswebrenderinggsp

how to render a response to a specific place on a gsp grails


I am new to grails and groovy. I am trying to find out how to render a response from an action in a grails controller IN THE SAME gsp - but in a DIFFERENT SECTION OF THE gsp - as the gsp that takes the request (in this case a web form gsp page)?

any links or turorials or just straight to the point "do this" kind of replies are welcomed.

I tried to google search for it, but I was not sure what to use as a search term and I could not find a concise answer.

========= UPDATE TO QUESTION (INCLUDING SOME CODE) =========

Here is the code I am working with. It is a Grails application in which I am using a bootstrap template (available for free on the internet of course).

Now the actual code itself for functionality works. What I am having an issue with is this:

I have a gsp page that uses a css template and another gsp temlate for layout. I can start the page as inside the gsp+css tempate using this code (snippet) in my gsp page:

<g:layoutBody/>

This allows me to call my calling controller code in this gsp file for the request:

<body>
<g:form name="form" controller="apiStart" id="form">
    <div><g:select name="protocolType" value="restCall" from="${["-select-", "GET", "POST", "PUT", "DELETE"]}"/> &nbsp <label>URL: </label><g:textField name="url" value="${url}" />
    &nbsp <label>username: </label><g:textField name="userName" value="${userName}" />  &nbsp <label>password: </label><g:textField name="passWord" value="${passWord}" /></div>

    %{--<div class="text-field"><label>URL: </label><g:textField name="url" value="${url}" /></div>--}%
    %{--<div class="text-field"><label>username: </label><g:textField name="userName" value="${userName}" /></div>
    <div class="text-field"><label>password: </label><g:textField name="passWord" value="${passWord}" /></div>--}%
    <br>
    <div><label>Use Advanced Parameters?</label><g:checkBox name="useAdvParms" value="${false}" /></div>
    <div class="text-field"><label>Header1: </label><g:textField name="header1" value="${header1}" /> &nbsp <label>Value1: </label><g:textField name="value1" value="${header2}" /></div>
    %{--<div class="text-field"><label>Value1: </label><g:textField name="value1" value="${header2}" /></div>--}%
    <div class="text-field"><label>Header2: </label><g:textField name="header2" value="${header3}" /> &nbsp <label>Value2: </label><g:textField name="value2" value="${header4}" /></div>
    %{--<div class="text-field"><label>Value2: </label><g:textField name="value2" value="${header4}" /></div>--}%
    <br>
    <div class="submit"><g:actionSubmit value="Submit" action="save"/></div>
</g:form>
</body>

And then this gsp code for the response:

<body>
<h3>API Test Results</h3>
API Tested: ${apiStart.url}, Response: ${apiStart.response3}
<br>
%{--<g:textArea name="myField" value="myValue" rows="20" cols="100"/>--}%
<div class="textarea"><label>Output</label><br><g:textArea name="myField" value="${apiStart.result3}"  />
</div>
%{--Responce Code: ${apiStart.response3}<br>--}%
%{--Response: <br> ${apiStart.result3} <br>--}%

</body>

My issue: it works fine as separate pages. I want to render the results of the request on the same page as the calling request.

in the screen shot attached: I want to put the results in the text box where it says "Output Displayed here...." screenshot of gsp page

I assumed templates in grails is the way to go about it. but I get a Java Null pointer exception when I try to insert the template into that part of the code.

Can someone advise and show me the best way to do this?

==================== END of Updated question ===================

thanks.

ironmantis7x


Solution

  • You can try using Grails templates. Basically Template is a (reusable) part of a View.
    Info:
    So you create template file bookTemplate.gsp, put all the gsp/html code in there as usual (but just the part, if it will be used in the body, then don't add html, body, head tags etc.
    Example:

    <div class="book" id="${book?.id}">
       <div>Title: ${book?.title}</div>
       <div>Author: ${book?.author?.name}</div>
    </div>
    

    Then you render that template into the gsp page in places where you want them to be (with a simple grails render tag. And it will simply get compiled (as the code from template would be pasted into gsp view).
    Render:

    <g:render template="bookTemplate" model="[book: myBook]" />