开发者

grails & MVC form architecture - use two actions per form .. a general industry pattern?

开发者 https://www.devze.com 2023-04-10 13:35 出处:网络
I\'m a bit new to Grails and also Web MVC in general.Looking at the scaffolded grails architecture, and extending that to building forms for an end-user web app, the pattern seems to be to devote two

I'm a bit new to Grails and also Web MVC in general. Looking at the scaffolded grails architecture, and extending that to building forms for an end-user web app, the pattern seems to be to devote two actions to a page that collects form data: the first to display the page, including any errors from previous submits, and the second to act on the data on submission, producing errors and a) returning to the first action as need be, or b) going on to a successor action/controller if no errors.

My question is, is this an "industry standard" type of pattern ... should it be?

Or might one alternatively make this work via one action in some cases ... or perhaps use 3 actions in other cases, e.g. perhaps one for the display of errors?

P.S. Please ignore my comment below; I updated this question quite a bit

--------- for purposes of discussion and for those reading with a Web MVC background but not a grails background, here's the grails generated code (version 1.3.7)

def create = {
    def personInstance = new Person()
    personInstance.properties = params
    return [personInstance: personInstance]
}

def save = {
    def personInstance = new Person(params)
    if (personInstance.save(flush: true)) { // my additon: save causes validation to be performed against user specified constraints, returning true or false
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'person.label', default: 'Person'), personInstance.id])}"
        redirect(action: "show", id: personInstance.id)
    }
    else {
        render(view: "create", model: [personInstance: personInstanc开发者_如何学Goe])
    }
}


I would say at this point it is industry standard. Most of the Java web frameworks implement the idea of forwarding to the view rather than exposing the view technology by having a user navigate to a .jsp/.gsp/.html page. I believe this became more of a standard when scriptlets (java code in the view) started to become a huge anti-pattern. At that point, the only way to get data into the view was to get the data on the server, then forward that data to the view.

Even when we redirect, we redirect to an action on the server which forwards to the view. So to your point, you thoughts are accurate that you'll generally have at least 2 actions. Although, depending on the complexity of your application, you'll end up with a handful more. For example, your scaffolded CRUD will contain 4 (index, save, update, delete)

0

精彩评论

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