我编写了一个API,该API使用vertx和内存数据库模型作为一堆json请求响应API诞生。效果很好。
然后有人想要漂亮的HTML显示以查看该应用程序中的某些内容。
所以我创建了一个Grails应用程序来尝试为我做简单的脚手架视图-但当然没有域模型本身]。
我创建了一个remoteRequestController-调用在我的“核心应用程序”上执行JSON并将其转换为对象的服务。
因此,对于我的列表请求-我执行GET并获取一个json数组。我将其转换为本地“非域”对象的列表,并在结果对象中构造图。
我的服务将列表返回给我的控制器-后者试图将地图置于视图中。
我尝试遵循grails约定-我的控制器称为'RemoteServiceRequestController',它调用RemoteServiceRequestService(执行远程获取/响应,并格式化为对象)-并且该视图使用变量在'remoteServiceRequest / list'中像这样'remoteServiceRequestList'
在那个gsp中,我从模型映射'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>
在我的控制器中,我尝试做出这样的响应
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 }
您可以从评论中看到,尝试了几种无效的方法。该视图不会将响应呈现为HTML,因为我认为该视图会自动假定变量是域对象列表。列表构建良好,我得到了期望的值-但我构建的对象数组仅来自src / main / groovy中的类。
我似乎无法在GSP视图中访问模型映射变量(除非它们是域对象),这不是我在这里所需要的。
响应方法非常固定,试图在内部巧妙地查看类型和内容-并在特定名称的映射中构建变量(在逻辑上类似<domainClass>List
-我尝试使用我的远程结果列表,但无法在GSP视图中呈现数据。
我可以使用GSP渲染模型映射中包含的变量的HTML,其中数据对象不是bean(例如我的请求数组),如果是的话,我该怎么做? (我可以将对象制成bean,但不确定是否会有所帮助)。
否则,我将不得不进行一些可怕的原始html处理,而仅使用render将其返回给浏览器-而不是使用GSP管道为我完成大部分艰苦的工作。
我希望这会变得直截了当,并且变成了与框架争执的噩梦。
我并不是真正的HTML演示天才(这就是为什么脚手架本来就足够好)的原因-但由于无法在gsp视图中呈现来自地图数据的响应而陷入困境。
如何使它起作用?或者,如果我能使脚手架起作用,有没有其他替代方法会足够好?
我编写了一个API,该API使用vertx和内存数据库模型作为一堆json请求响应API诞生。工作良好。然后有人想要漂亮的HTML显示,以查看其中的某些内容...
问题我可以使用GSP渲染模型中包含的变量的HTML吗?映射数据对象不是bean的地方(例如我的数组要求),如果可以的话,该怎么办?