Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Revel Results


May 15, 2021 Revel


Table of contents


The controller method must return a revel.Result which handles response results, has an interface defined as follows:

type Result interface {
    Apply(req *Request, resp *Response)
}

revel.Controller the following methods to process response results:

  • Render, RenderTemplate - Render template, transfer parameters.
  • RenderJson, RenderXml - Serialize structures to json or xml.
  • RenderText - Returns a clear text response.
  • Redirect - Redirect to a controller method or URL
  • RenderFile - Returns a file, generally used in response to file downloads.
  • RenderError - Render errors/500.html template to return a 500 error.
  • NotFound - Render errors/404.html template to return a 404 error.
  • Todo - Return a sting response (500)

In addition, developers can customize the level. Result.

Set the status code/content type

Each built-in Result has a default status code and content type that can be modified by simply setting related properties:

func (c App) Action() revel.Result {
    c.Response.Status = http.StatusTeapot
    c.Response.ContentType = "application/dishware"
    return c.Render()
}

Rendering

Controller rendering method (e.g. "Controller.Action"), mvc.Controller.Render does two things:

  1. Add parameters to the controller's RenderArgs, using the name of the variable as the key of the dictionary.
  2. Render the template "I want to use the transferred views/Controller/Action.html."

If it is not successful (for example, no template was found), ErrorResult will be returned.

This allows developers to write:

func (c MyApp) Action() revel.Result {
    myValue := calculateValue()
    return c.Render(myValue)
}

Refer to the variable "myValue" in the template. This is usually more convenient than building a clear map, because in many cases the data will be treated as a local variable.

Note: R evel determines which template to use by calling the controller method name, looking for the parameter name. Therefore, c.Render() can only be called by operation.

RenderJson / RenderXml

An application can RenderJson or RenderXml and transfer any type of variable (usually a struct). R evel uses json.Marshal or xml.Marshal serializes variables.

If app.conf profile results.pretty=true it will be serialized using MarshalIndent to produce nice indentations for ease of use.

Redirect

A secondary function is used to generate redirects. It can be used in two ways:

  1. Redirect to a controller method (without parameters):
    return c.Redirect(Hotels.Settings)

This form is useful because it provides a degree of route type security and independence (automatically generating URLs).

  1. Redirect to a formatted string:
    return c.Redirect("/hotels/%d/settings", hotelId)

It is usually used to transmit parameters.

It returns a 302 (temporary redirect) status code.

Add your own Results

Here is an example of adding simple results.

Create this type:

type Html string

func (r Html) Apply(req *Request, resp *Response) {
    resp.WriteHeader(http.StatusOK, "text/html")
    resp.Out.Write([]byte(r))
}

Use it in a controller method:

func (c *App) Action() revel.Result {
    return Html("<html><body>Hello World</body></html>")
}

The status code

Each Result sets a default status code, and you can reset the default status code:

func (c *App) CreateEntity() revel.Result {
    c.Response.Status = 201
    return c.Render()
}