redirect(action: "show")
redirect(controller: "book", action: "list")
redirect(controller: "book", action: "list", namespace: "publishing")
redirect(controller: "book", action: "list", plugin: "publishingUtils")
redirect(action: "show", id: 4, params: [author: "Stephen King"])
redirect(controller: "book", action: "show", fragment: "profile")
redirect(uri: "book/list")
redirect(url: "https://www.blogjava.net/BlueSUN")
redirect(Book.get(1))
// For 302 Moved Temporarily
redirect(action: "foo", permanent: false)
// For 307 Temporary Redirect
redirect(action: "foo", permanent: false, moved: false)
// For 301 Moved Permanently Redirect (moved is default true)
redirect(action: "foo", permanent: true)
// For 308 Permanent Redirect
redirect(action: "foo", permanent: true, moved: false)
redirect
Purpose
To redirect flow from one action to the next using an HTTP redirect.
Examples
Description
Redirects the current action to another action, optionally passing parameters and/or errors. When namespace is omitted, Grails resolves the target controller namespace automatically. If the target is the current controller, Grails uses the current request namespace. If exactly one controller has the target name, Grails uses that controller’s namespace, whether the controller is namespaced or not. You only need to set namespace when more than one controller shares the same name. To issue a redirect from a namespaced controller to a controller that is not in a namespace, specify namespace: null as shown below. By default, the redirect code will be MOVED_TEMPORARILY (302).
class SomeController {
static namespace = 'someNamespace'
def index() {
// issue a redirect to PersonController which does not define a namespace
redirect action: 'list', controller: 'person', namespace: null
}
}
Parameters
redirect(Map params)
-
action(optional) - the name of the action to use in the link, if not specified the default action will be linked -
controller(optional) - the name of the controller to use in the link, if not specified the current controller will be linked -
namespace(optional) - the namespace of the controller to redirect to. If omitted, the namespace is inferred from the target controller; you only need to set it when more than one controller shares the name. Usenamespace: nullto target a non-namespaced controller. -
plugin(optional) - the name of the plugin which provides the controller -
id(optional) - the id to use in the link -
fragment(optional) - The link fragment (often called anchor tag) to use -
mapping(optional) - The named URL mapping to use to rewrite the link -
params(optional) - a map containing request parameters -
url(optional) - a map containing the action, controller, id, etc. -
absolute(optional) - Iftrue(default) will prefix the link target address with the value of thegrails.serverURLproperty fromapplication.yml, or http://localhost:8080 if there is no value inapplication.ymland not running in the production environment. Iffalsea partial URI is generated for theLocationheader. -
base(optional) - Sets the prefix to be added to the link target address, typically an absolute server URL. This overrides the behaviour of theabsoluteproperty if both are specified. -
permanent(optional) - Defaults tofalse. Iftrue, the redirect will be issued with a PERMANENT style HTTP status code. Ifmovedis true: MOVED_PERMANENTLY (301). Otherwise, PERMANENT_REDIRECT (308). -
moved(optional) - Defaults totrue. Whentrue, the redirect will be issued with a MOVED style HTTP status code. Ifpermanentis true: MOVED_PERMANTENTLY (301). Otherwise, MOVED_TEMPORARILY (302).
Domain Class
redirect(Object domainClass)
A special case is if a domain class is passed into redirect, it will use the LinkGenerator to create the URL. For example, redirect(Book.get(1)) will generate a redirect to /book/show/1.