Announcement Announcement Module
Collapse
No announcement yet.
HELP - Common Controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HELP - Common Controller

    Hi,

    I am using Spring 3 and Spring Security 2.05 in my web application and I am looking for a way in which to get a method executed for each page request that comes in. Basically I have some common information that needs to be available within each page. To give an example, this could be the number of users currently logged in. I know how to get this information, but I am struggling with how to make it available to each page.

    First of all I tried creating a mapping of '/**' on one of my controllers but that didn't seem to work.

    I got a hint from another user on this forum that perhaps I should create a 'BaseController' which all other controllers extend and to put my common information in there. That seemed like a logical idea, but I couldn't work out how to setup the ModelAttribute so that it was available to any JSP that was being prepared. Also, if I had a common method in the BaseController required for each request and then a method in the called controller which also required execution, only the method in the called controller seemed to get executed.

    Some poeple also seem to suggest that using HandlerInterceptors is the right way to go. The only problem with that is I have no idea how you would get the ModelAttribute and Binding to work. I need this common information to be available to each JSP in a defined model attribute.

    Can anyone give me an example of the correct way to go about this?

    Many Thanks
    Paul

  • #2
    A HandlerInterceptor is a definitely good solution to your problem.
    If the information you want to display is not dependent on request data, you can use the postHandle method. One of the arguments is a ModelAndView object. Put your information into the model and you can access the data inside JSPs as request attribute via EL (eg. ${the_attribute_name_goes_here}).
    On how to configure HandlerInterceptors you should find enough information in the reference documentation.

    HTH

    Comment


    • #3
      Hi,

      Thanks for your reply

      I've been looking at the docs and pretty much followed the example in there in terms of configuration.

      I put this into my securityContent.xml file

      Code:
          <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
              <property name="interceptors">
                <list>
                  <ref bean="commonInfoInterceptor"/>
                </list>
              </property>
              <property name="mappings">
                  <value>/**</value>
              </property>
          </bean>
          
          <bean id="commonInfoInterceptor" class="my.package.CommonInfoInterceptor"/>
      and created a basic implementation just to test if it is working

      Code:
      package my.package;
      
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
      
      public class CommonInfoInterceptor extends HandlerInterceptorAdapter {
      	
      	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      		
      		System.out.println("====================================================");
      		System.out.println("IN COMMON INFO INTERCEPTOR!!");
      		System.out.println("====================================================");
      		
      		return true;
      	}
      }
      However, it doesn't seem to work, I get the following error in the logs

      Code:
      Mar 18 2010 10:11:21 ERROR org.springframework.web.context.ContextLoader - Context initialization failed
      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'handlerMapping' defined in ServletContext resource [/WEB-INF/spring/security-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '' is defined
      What am I doing wrong?

      Regards,
      Paul.

      Comment


      • #4
        I suggest the reference guide... The mappings element is for mapping incoming requests to controllers it isn't to specify mappings for the interceptor.

        If you are using @Controller configure a DefaultAnnotationHandlerMapping and register your interceptor with that.

        Comment

        Working...
        X