Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access controller dynamic properties within a base controller's constructor in Grails?
    primarykey
    data
    text
    <p>Basically, I want to be able to assign objects created within filters to members in a base controller from which every controller extends. Any possible way to do that?</p> <p>Here's how I tried, but haven't got to make it work.</p> <p>What I'm trying to achieve is to have all my controllers extend a base controller. The base controller's constructor would be used to assign values to its members, those values being pulled from the <em>session</em> map. Example below.</p> <p>File grails-app/controllers/HomeController.groovy:</p> <pre><code>class HomeController extends BaseController { def index = { render username } } </code></pre> <p>File grails-app/controllers/BaseController.groovy:</p> <pre><code>abstract class BaseController { public String username public BaseController() { username = session.username } } </code></pre> <p>When running the app, the output shown is:</p> <pre><code>2010-06-15 18:17:16,671 [main] ERROR [localhost].[/webapp] - Exception sending context initialized event to listener instance of class org.codehaus.groovy.grails.web.context.GrailsContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Unable to locate constructor with Class parameter for class org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass ... Caused by: java.lang.RuntimeException: Unable to locate constructor with Class parameter for class org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass ... Caused by: java.lang.reflect.InvocationTargetException ... Caused by: org.codehaus.groovy.grails.exceptions.NewInstanceCreationException: Could not create a new instance of class [com.my.package.controller.HomeController]! ... Caused by: groovy.lang.MissingPropertyException: No such property: session for class: com.my.package.controller.HomeController at com.my.package.controller.BaseController.&lt;init&gt;(BaseController.groovy:16) at com.my.package.controller.HomeController.&lt;init&gt;(HomeController.groovy) ... 2010-06-15 18:17:16,687 [main] ERROR core.StandardContext - Error listenerStart 2010-06-15 18:17:16,687 [main] ERROR core.StandardContext - Context [/webapp] startup failed due to previous errors </code></pre> <p>And the app won't run.</p> <p>This is just an example as in my case I wouldn't want to assign a username to a string value, but rather a few objects pulled from the <em>session</em> map. The objects pulled from the <em>session</em> map are being set within filters.</p> <p>The alternative I see is being able to access the controller's instance within the filter's execution. Is that possible?</p> <p>Please help! Thanks a bunch!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload