Dave
Dave

Reputation: 8937

Grails: Can't figure out why I'm getting a 404

I'm using Grails 1.3.6. I have this file ...

grails-app/views/home/design/index.gsp

Here is what is defined in my HomeController. Sadly, whenever I visit, "http://localhost:port/context-path/design/", I get a 404 error. The server starts normally and there are no errors in the logs. What can I do to get my page instead of the 404?

def index = {
    def folder = params.folder;
    def page = params.page;

    if (page) { 
        try { 
            def contents = IOService.getFileContents(folder, page)  
            response.setContentType("application/json")
            response << contents
        } catch (FileNotFoundException e) { 
            response.status = 404;
        }   // try
    } else { 
        render(view: "/home/${folder}/index") 
    }   // if
}

My URLMappings file consists of ...

static mappings = {
    "/$folder?/$page"{
        controller = "home"
        action = "index"
    }

    "/"(view:"/index")
    "500"(view:'/error')
}

Thanks, - Dave

Upvotes: 0

Views: 5249

Answers (2)

Randyaa
Randyaa

Reputation: 2945

Since it seems you have two distinct actions, I would set things up a bit differently:

def indexWithPage = {
    def folder = params.folder;
    def page = params.page;

    try { 
        def contents = IOService.getFileContents(folder, page)  
        response.setContentType("application/json")
        response << contents
    } catch (FileNotFoundException e) {
        e.printStackTrace(); 
        response.status = 404;
    }   // try
}

def index
    def folder = params.folder;

    render(view: "/home/${folder}/index") 
}

with a URLMaping of:

static mappings = {
    "/$folder/$page"{
        controller = "home"
        action = "indexWithPage"
    }

    "/$folder"{
        controller = "home"
        action = "index"
    }

    "/"(view:"/index")
    "500"(view:'/error')
}

I also threw a e.printStackTrace(); in there to help us determine whether you're getting YOUR 404 or the action is truely not being called.

Upvotes: 1

Rob Hruska
Rob Hruska

Reputation: 120446

If you want to be able to access

/context-path/home/design

Your action needs to be named design, i.e.

class HomeController {
    def design = {

    }
}

The Grails convention is always /context-path/controllerName/actionName (unless you have it mapped differently in grails-app/conf/URLMappings.groovy).

Your example's a bit unclear which path you're trying to access. To address both:

  • If you want /context-path/design, you need a DesignController with an index action (because if no action is supplied in the URL, Grails looks for the index action).
  • If you want /context-path/home/design, you need a HomeController with a design action.

Edit:

In the comments, you express the want to be able to have /context-path/design map to the HomeController index action. You can do this with grails-app/conf/URLMappings.groovy:

"/design"(controller: 'home', action: 'index')

Upvotes: 6

Related Questions