Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:06:26+00:00 2026-05-27T05:06:26+00:00

I am looking for ways on how to cleanup my Grails controller code. In

  • 0

I am looking for ways on how to cleanup my Grails controller code. In various controllers i more or less have the same logic..

  • get the object
  • check if it exists
  • etc..

Is there a suggested way on making controller actions reuse common code?

— solution —

All answers to the question have contributed to the solution we have implemented.

We created a class that is used in our controllers using the Mixin approach. One of the methods that the mixin exposes is the withObject method. This method takes the domainname from the controller and uses this a base for the method. This behaviour can be overridden of course!

def withObject(object=this.getClass().getName()-"Controller", id="id", Closure c) {
    assert object
    def obj =  grailsApplication.classLoader.loadClass(object).get(params[id])
    if(obj) {
        c.call obj
    } else {
        flash.message = "The object was not found"
        redirect action: "list"
    }
}

So all answers have contributed to the solution! Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:06:27+00:00Added an answer on May 27, 2026 at 5:06 am

    I always pull out this blog post when this question comes up:

    http://mrpaulwoods.wordpress.com/2011/01/23/a-pattern-to-simplify-grails-controllers/

    Basically you have a private helper for various domains in your controllers.

    private def withPerson(id="id", Closure c) {
        def person = Person.get(params[id])
        if(person) {
            c.call person
        } else {
            flash.message = "The person was not found."
            redirect action:"list"
        }
    }
    

    The way you code the getter is very flexible and a typical use for me (that is not covered in the blog) is for editing etc.

    I normally code this way (i like the pattern for its clear division and readability):

     def editIssue() {
        withIssue { Issue issue ->
            def issueTypes = IssueTypeEnum.values().collect {it.text }
            [issueTypes:issueTypes,activePage:"issue", issue: issue]
        }
    }
    
     def doEditIssue(IssueCommand cmd) {
        if(cmd.validate()) {
            withIssue { Issue issue ->
                issue.updateIssue(cmd)
                redirect(action: "show", id: issue.id)
            }
        }
        else {
            def issueTypes = IssueTypeEnum.values().collect {it.text }
            render(view: "edit", model:[issueTypes:issueTypes,issue:cmd,activePage:"issue"])
        }
    }
    

    With my getter helper being:

    private def withIssue( Closure c) {
        def issue = Issue.get(params.id)
        if(issue) {
            c.call issue
        }
        else {
            response.sendError(404)
        }
    }
    

    I do think that the mixin method (very similar to the ‘extend a common abstract controller’ way) is nice too, but this way gives two advantages:

    1. You can type the helper, like you see I do in the closure giving you access to the methods etc in STS/IDEA (not tested Netbeans)
    2. The repetition is not very high, and the ability to change the getter (to use for example BarDomain.findByFoo(params.id) etc)

    In the view I bind to edit() I just put an id="${issue.id}" in the <g:form> and it works seamlessly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for ways to release two android apps with same source code
I've been looking at ways to make a table have a fixed header especially
I’m looking at ways to improve the consistency, brevity, and readability of some code
I'm looking for ways to obfuscate the following c#/silverlight source code: if (searchBox.Text.Equals(string literal)){
i'm looking for ways to display and edit source code (ala google docs) in
I am looking at ways of interfacing to specific hardware I/O addresses from various
I have been looking for ways to set my Django form to only accept
I've been looking for ways to measure the complexity of my projects's source code,
I'm always looking for ways to be more productive, and I've been reading a
I am looking for ways of making the following more concise. public class MyTests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.