Showing posts with label GSP. Show all posts
Showing posts with label GSP. Show all posts

Friday, February 8, 2019

GRAILS : Rendering Groovy GSP Code From A String | Grails – Rendering a Template from a String | Render Grails template from external source




package com.pkm.utils

import grails.util.Holders
import groovy.text.Template
import org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine
import org.springframework.core.io.ByteArrayResource
import org.springframework.web.context.request.RequestContextHolder

/**
 * Created by pritom on 29/11/2017.
 */
class InvoicePdfUtils extends UtilsBase {
    static def test() {
        GroovyPagesTemplateEngine engine = Holders.applicationContext.getBean(GroovyPagesTemplateEngine)
        StringWriter stringWriter = new StringWriter()

        String content = "CONTENT-\${a}--\${b.none?.none ?: 'None'}-MINE-XXX " +
                "<ui:serverURL/> <g:if test='true'>True</g:if> HERE??? YES ME IS HERE, ".toString()
        Map model = [a: '10', b: [:]]

        String pageName = engine.getCurrentRequestUri(RequestContextHolder.getRequestAttributes().request) // /grails/test.dispatch
        Template template = engine.createTemplate(new ByteArrayResource(content.getBytes("UTF-8"), pageName), pageName, true)
        Writable renderedTemplate = template.make(model)
        renderedTemplate.writeTo(stringWriter)

        println("Request-path=${engine.getCurrentRequestUri(RequestContextHolder.getRequestAttributes().request)}")
        println(stringWriter.toString())
    }
}


This code block will take input from yourself and then parse as a GSP parser and return output as String.

https://little418.com/2009/11/rendering-groovy-gsp-code-from-a-string.html

Wednesday, October 18, 2017

Grails Bind Function | Method For Groovy Page View

Grails Bind Function | Method For Groovy Page View

Have to extend metaclass as below:


import org.codehaus.groovy.grails.web.pages.GroovyPage

GroovyPage.metaClass.with {
    xor = {
        "XOR-VALUE"
    }
    isTrue = { Map attrs = null ->
        println(attrs)
        return true
    }
}

And then you can use function/method in gsp page as below:


<h1>${xor()} ${isTrue(param1: 'value1', params2: 'value2') ? 'IS-TRUE' : 'IS-FALSE'}</h1>
<-- --="">

And output is as below:

XOR-VALUE IS-TRUE


Wednesday, January 8, 2014

Grails, finding available views (.gsp's) at runtime

This is not a important post. I just need to do some googling to achieve this and i found at: http://stackoverflow.com/questions/1577872/grails-finding-available-views-gsps-at-runtime. Thanks Burt Beckwith. I put this here to not search in google again.


import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH

def viewGsp() {
    List gspFileList = []
    if (grailsApplication.isWarDeployed()) {
        findWarGspList ('/WEB-INF/grails-app/views', gspFileList);
    } else {
        findDevGspList ('grails-app/views', gspFileList);
    }
    gspFileList.each {
        render it.toString() + "<br/>";
    }
    render "";
}
private void findDevGspList(String location, List gspFileList) {
    for (file in new File(location).listFiles()) {
        if (file.path.endsWith('.gsp')) {
            gspFileList << file.path - 'grails-app/views/';
        } else {
            findDevGspList (file.path, gspFileList);
        }
    }
}

private void findWarGspList(String location, List gspFileList) {
    def servletContext = SCH.servletContext
    for (path in servletContext.getResourcePaths(location)) {
        if (path.endsWith('.gsp')) {
            gspFileList << path - '/WEB-INF/grails-app/views/';
        } else {
            findWarGspList (path, gspFileList);
        }
    }
}

Sunday, July 28, 2013

How do I call/use Grails service from a gsp

We can use 'grailsApplication.classLoader.loadClass' to load service.

<%@ page import="com.myproject.MyService" %>
<%
def myService = grailsApplication.classLoader.loadClass('com.myproject.MyService').newInstance()
%>
<span>${myService.methodName()}</span>


We can use another approach

In Grails we can use the set tag to define a variable with a value on a GSP. But we can also use the same tag to access a Grails service on a GSP. We must use the bean attribute with the name of the Grails service. We can even pass the name of any Spring managed bean in the application context of our application.
In the following sample GSP we access a Grails service with the name BlogService. We invoke the method allTitles() and use the result.

<html>
<head>
<meta content="main" name="layout"/>
</head>
<body>
    %{--Use BlogService--}%
    <g:set var="blog" bean="blogService"/>
    <ul>
    <g:each in="${blog.allTitles()}" var="title">
        <li>${title}</li>
    </g:each>
    </ul>
</body>
</html>