Skip to content

Commit

Permalink
Grails REST render context support viewName including namespace and c…
Browse files Browse the repository at this point in the history
…ontroller name

Better support integration with other View Templates, related commit d9a841e
  • Loading branch information
rainboyan committed Apr 24, 2023
1 parent 16335a2 commit 108548c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -94,6 +94,11 @@ interface RenderContext {
*/
String getViewName()

/**
* @return The default view name to use
*/
String getDefaultViewName()

/**
* The model to use for the response
* @param model The model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ abstract class AbstractLinkingRenderer<T> extends AbstractIncludeExcludeRenderer
MimeType mimeType = context.acceptMimeType ?: getMimeTypes()[0]
context.setContentType(GrailsWebUtil.getContentType(mimeType.name, encoding))

String viewName = context.viewName ?: context.actionName
String viewName = context.viewName ?: context.defaultViewName
GroovyPageScriptSource view = groovyPageLocator?.findViewForFormat(context.controllerName, viewName, mimeType.extension)
if (view) {
// if a view is provided, we use the HTML renderer to return an appropriate model to the view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ class ServletRenderContext extends AbstractRenderContext {
modelAndView ? modelAndView.viewName : null
}

@Override
String getDefaultViewName() {
String namespace = getControllerNamespace()
String controller = getControllerName()
String viewName = getActionName()
if (namespace) {
viewName = namespace + '/' + controller + '/' + viewName
}
else {
viewName = controller + '/' + viewName
}
viewName
}

protected ModelAndView getModelAndView() {
HttpServletRequest request = this.webRequest.currentRequest
ModelAndView modelAndView = (ModelAndView) request.getAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DefaultHtmlRenderer<T> implements Renderer<T> {
}

if (!context.viewName) {
context.setViewName(context.actionName)
context.setViewName(context.defaultViewName)
}

if (object instanceof Errors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class DefaultJsonRenderer<T> implements Renderer<T> {
void render(T object, RenderContext context) {
MimeType mimeType = context.acceptMimeType ?: MimeType.JSON
context.setContentType(GrailsWebUtil.getContentType(mimeType.name, encoding))
String viewName = context.viewName ?: context.actionName
String viewName = context.viewName ?: context.defaultViewName
GroovyPageScriptSource view = groovyPageLocator?.findViewForFormat(context.controllerName, viewName, mimeType.extension)
if (view && !(object instanceof Errors)) {
// if a view is provided, we use the HTML renderer to return an appropriate model to the view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class DefaultXmlRenderer<T> implements Renderer<T> {
MimeType mimeType = context.acceptMimeType ?: MimeType.XML
context.setContentType(GrailsWebUtil.getContentType(mimeType.name, encoding))

String viewName = context.viewName ?: context.actionName
String viewName = context.viewName ?: context.defaultViewName
GroovyPageScriptSource view = groovyPageLocator?.findViewForFormat(context.controllerName, viewName, mimeType.extension)
if (view) {
// if a view is provided, we use the HTML renderer to return an appropriate model to the view
Expand Down

0 comments on commit 108548c

Please sign in to comment.