开发者

Grails action works for GET request, returns 404 for POST request

I'm learning to use Grails and have run into a situation I don't understand when handing what should be a simple form submission.

I have created a controller called 'add' (there is an AddController.groovy source file and an appropriate add/index.gsp view) and have defined a very sparse 'process' action which currently renders a small amount of HTML to verify that the action is being called.

The URL for the process action on the add controller is (not surprisingly) http://localhost:8080/frontend/add/process/.

I would like to submit a very simple form to the process action as a first step towards integrating with some existing Java libraries.

Sending a GET request to http://localhost:8080/frontend/add/process/ causes the process action to be called and the browser to display the relevant simple HTML content.

Sending a POST request to http://localhost:8080/frontend/add/process/ returns a HTTP 404 error.

I appreciate I'm missing some fundamental addition to my application such that the above action works with both GET and POST requests. I had assumed by default the request type would not matter.

I'd be very happy at this stage if I can send a POST request to the appropriate action and have some markup rendered just to demonstrate that things are working.

What fundamentally essentials piece of the puzzle am I missing?

controllers/frontend/AddController.groovy:

package frontend

class AddController {

    def index = { }

    def process = {
        render "<h1>process action being performed</h1>"
    }
}

views/add/index.gsp

<html>
    <head>
        <title>Test View for index action</title>
        <meta name="layout" content="main" />
    </head>
    <body>
    <g:form controller="add" action="process">
        <label for="title">Title:</label>
        <g:textField name="title" id="title" />
        <label for="content">Content:</label>
        <g:textArea name="content" id="content" />
        <g:actionSubmit value="Add" />
    </g:form>
    </body>
&l开发者_如何学编程t;/html>


The <g:actionSubmit /> directive needs an action attribute to indicate the action to be handled. I had assumed the form action would have been sufficient.

I needed to change:

<g:actionSubmit value="Add" />

to:

<g:actionSubmit value="Add" action="process" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜