Steven Byks
Steven Byks

Reputation: 125

How do I set the content type in Grails?

I'm just trying to setup a very simple controller to return a very simple view (with a very simple layout), however I keep getting errors related to the content type.

class SimpleController {
    def index() {
        render(view: "simple")
    }
 }

_simple.gsp:

<html>
<head>
    <meta name="layout" content="blank" />
</head>
<body>
Simple test page.
</body>
</html>

blank.gsp:

<!DOCTYPE html>
<html>
<head>
    <g:layoutHead />
</head>
<body>
    <g:layoutBody />
</body>
</html>

It doesn't get much simpler than that. The controller method is found correctly, the issue seems to be during the rendering. Here's the error I'm getting:

2016-07-05 16:07:14,931 [http-bio-8085-exec-5] ERROR errors.GrailsExceptionResolver - NullPointerException occurred when processing request: [GET] /testApp/simple/ Stacktrace follows: Message: null Line | Method ->> 15 | in com.opensymphony.module.sitemesh.filter.HttpContentType


| 49 | build in com.opensymphony.sitemesh.compatability.PageParser2ContentProcessor | 89 | getContent . . . . . . . . . . in org.codehaus.groovy.grails.web.sitemesh.GrailsContentBufferingResponse | 107 | obtainContent in org.codehaus.groovy.grails.web.sitemesh.GrailsLayoutView

Debugging confirms that context.contentType is null. I've found four different way to set the content type, but none of them seem to work.

<%@ page contentType="text/html;charset=UTF-8" %>

At the top of the view.

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

In the head tag of the view.

contentType: "text/html"

Passed to the render method in the controller.

response.setContentType("text/html")

Called in the controller method.

This is in an existing project, where many other controllers and views work perfectly fine. I need to add a new controller and view, which I originally copied existing working pieces. When that didn't work, I tried to reduce the problem down to be as simple as possible, and ended up with the above code, which is still not working.

Btw, this is with Grails 2.5.1 (stuck with this version).

Upvotes: 0

Views: 2179

Answers (3)

susi
susi

Reputation: 493

As @jmallen said, the page should be named "simple.gsp". The underscore at the beginning is something I only know for templates, gsp-files, that could be included in other pages with the following statement:

<g:render template="/layouts/menu" />

than the gsp file for the menu is named

_menu.gsp

Upvotes: 0

Mario
Mario

Reputation: 4998

Could be even simpler and incidentally solve your problem.

class SimpleController {
    def index() {

    }
}

Rename _simple.gsp to index.gsp and needs to be located in views/simple/ directory. If blank.gsp its in views/layouts directory it should work now.

Upvotes: 1

jmallen
jmallen

Reputation: 71

What you have there should work.

If you're using the default URL mappings you should name your view simple.gsp instead of _simple.gsp

If it's not as simple as that you might try recreating a simple app.

Upvotes: 2

Related Questions