ali
ali

Reputation: 1496

Simple Hello World Grails Form Submission

I am new to Grails and I am trying to get a very simple example to work. I should just submit a form and display "Hello World" on the screen. It consists of the following controller:

package surface

class SearchController {

 def index() {
    render(view: "search")
 }

 def result() {
    render "Hello World"
 }
}

and a view, with the form:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<g:form name="searchform" url="result">
<g:textArea cols="80" rows="30" name="searchfield"/>
<g:actionSubmit value="Ask"/>
</g:form>
</body>
</html>

When I click on "Ask" I get a 404 error but the browser correctly accesses "/surface/search/result". When I enter that address directly without using the form the "Hello World" appears correctly. This is probably a no-brainer but I seem to be unable to find out why this does not work from the documentation.

Upvotes: 6

Views: 3119

Answers (1)

user800014
user800014

Reputation:

Complementing the @Tom Metz answer, what you need to keep in mind in the Grails controller structure is that every public method is considered an action. This action is mapped to a url. In your example will exists /search/index and /search/result (controller + action).

The documentation of the g.form is corret, since this says that;

url (optional) - A map containing the action,controller,id etc.

So to correct your view you can set the action as commented or you can adjust the way you use url:

<g:form name="myForm" url="[action:'result',controller:'search']">

Upvotes: 5

Related Questions