I've written an API that started life as a bunch of json request response apis, using vertx an inmemory db model. Works fine.
Then someone wanted nice HTML display to see some of the content in that app.
So I created a Grails app to try and do the simple scaffolding views for me - but of course there's no domain model per se.
I created a remoteRequestController - that calls a service that does a JSON get on my 'core app' and converts the data into objects.
So for my listing requests - I do a GET and get a json array back. I convert that a List of local 'non domain' objects and construct a graph in objects of the result.
My service returns the list to my Controller - which tries to put the map to a view.
I've tried to follow grails convention - my Controller is called 'RemoteServiceRequestController', which invokes a RemoteServiceRequestService (does the remote get/reponse, and format into objects) - and the view is in 'remoteServiceRequest/list' using a variable like so 'remoteServiceRequestList'
In that gsp I reference an item from the model map 'remoteServiceRequestList'
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main" />
<g:set var="entityName" value="${message(code: 'request.label', default: 'ServiceRequest')}" />
<title><g:message code="default.list.label" args="[entityName]" /></title>
</head>
<body>
<a href="#list-customer" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content…"/></a>
<div class="nav" role="navigation">
<ul>
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
<li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
</ul>
</div>
<div id="list-request" class="content scaffold-list" role="main">
<h1><g:message code="default.list.label" args="[entityName]" /></h1>
<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>
<p> "got map as ${params}: and" + "list as ${remoteServiceRequestCount}"</p>
<f:table collection="${remoteServiceRequestList}" />
<div class="pagination">
<g:paginate total="${remoteServiceRequestCount ?: 0}" />
</div>
</div>
</body>
</html>
In my controller I try and do a respond like this
def list () {
....
List<Request> rlist = remoteRequestService.bindJsonToServiceRequestList(json)
//return either an empty list or result of query, view will convert list to
println "remote list returned $rlist, size :${rlist.size()}"
//response rlist, [model: [remoteServiceRequestCount: rlist.size()] ]
//Map model = [remoteServiceRequestList: rlist, remoteServiceRequestCount: rlist.size()]
Map model = ["remoteServiceRequestList": ["will","marian"]]
//render (view:'list', model:model )
//response rlist, [model: [remoteServiceRequestInstanceCount: rlist.size(), remoteServiceRequestInstanceList: rlist]]
ModelAndView mv = new ModelAndView()
mv.addAllObjects(model)
mv
}
As you can see from comments tried several ways with nill effect. The view will not render the response as HTML as I think the view automatically assumes the variable is a list of domain objects. The list is constructed ok, and I get the values I expect - but the object array I build is just from classes from src/main/groovy.
I can't seem to access the model map variables in the GSP view (unless they are domain objects), which is not what I need here.
The response method is very pernickety, and tries to get clever internally looking at the type and content - and building an variable in the map of a specific name (logically something like <domainClass>List
- I've tried to set this explicitly with my remote result list, but I can't get it to render the data in the GSP view.
Can I use GSP to render HTML for variables held in the model map where the data objects are not beans (such as my array of requests) and if so how do i do that ? (I could make the objects into beans but not sure if that's going to help or not).
Otherwise I'm going to have to do some horrible raw html processing and just use render to return that to browser - rather than using the GSP plumbing to do most of the hard work for me.
I'd hoped this was going to be straight forward and its turned into a nightmare fighting the framework.
I'm not really a HTML presentation genius (which is why the scaffolding would have been good enough) - but stuck with this inability to render the response from data in map in gsp view.
How might this be made to work - or is there an alternative that will be good enough if I could have got scaffolds to work?