Prasanth Thyagarjan
Prasanth Thyagarjan

Reputation: 103

Controller is there, but showing "Error code 404 page not found" in grails

new.gsp:

<html>
<head>
</head>
<body>
      <g:form name="myForm" controller="New" action="index" params="username:username">
      <div>
          <fieldset class="form">
               <label for="name">Name:</label>
               <div>
               <g:textField name="username" value="${params.userName}"/>
               </div>
          </fieldset>
      </div>
      <g:submitButton name="Submit" value="Submit" />
      </g:form>
</body>
<html>

NewController.groovy:

package sample
import com.sun.org.apache.bcel.internal.generic.NEW;

class NewController {
    def index = {
        if($params.userName){
            render(view:"/login.gsp")
        }
    }
}

login.gsp is a simple page, having a simple welcome note.

if some body the solution please reply,

thanks in advance. by prasanth

Upvotes: 2

Views: 7102

Answers (5)

PassionateProgrammer
PassionateProgrammer

Reputation: 864

Change your controller name to "new" instead of New in It will work.

Or else you can modify your "save" action in controller, so that when you click on save button new page will be rendered.

Upvotes: 1

john Smith
john Smith

Reputation: 17906

if your action is called index, you can acces the page on

localhost:8080/webapp/NewController

Upvotes: 0

codelark
codelark

Reputation: 12334

There are a few issues in the posted code that will cause you problems:

  1. You're accessing the parameters with $params instead of params. The $ character is only necessary when you are in a GString. e.g. def foo = "your username is ${params.userName}"

  2. Your view is named new.gsp but your action is named index. Grails will by default look for a view matching the action name in a directory named for the controller. In other words, since you don't tell it explicitly to render /new.gsp grails will look for /new/index.gsp. You can either rename the view to /new/index.gsp or tell grails to render the view new in the index action.

  3. When attempting to render your logged in page, you're calling render(view: 'login.gsp'). The gsp extension is not necessary when calling the render tag. You're intended to use the grails view name, not the filename. render(view: 'login')

  4. If you're using a recent version of grails (>2.0) you should be using controller methods rather than closures. e.g. def actionName() { } as apposed to def actionName() = { }. The reasoning is in the grails documentation.

Here's what it could look like with all the issues addressed:

rename new.gsp to /new/index.gsp. rename login.gsp to /new/loggedIn.gsp.

controller:

class NewController {
    def index() {
        if (params.userName) {
            forward action: 'loggedIn'
            return // render and forward don't end flow control
        }
    }

    def loggedIn() {} // no logic, automatically renders '/new/loggedIn.gsp'
}

Upvotes: 1

founddrama
founddrama

Reputation: 2411

If the view file is new.gsp then you need your action to also be new or else have a URL mapping (in UrlMappings.groovy) to do something like:

"/new" {
    controller = 'new'
    action = 'new'
}

Or you can set

static defaultAction = 'new'

...in your NewController.

Then Grails will find the appropriate action on your controller.

Upvotes: 0

Bart
Bart

Reputation: 17361

Add a handler to your controller named login.

def login = {}

Upvotes: 0

Related Questions