Results 1 to 9 of 9

Thread: automatically calling another method from a method

  1. #1

    Default automatically calling another method from a method

    is there a way to have a controller method to automatically call a another method.

    i am asking this because i am planning to create a base controller where in all extending controller, i need to log some http header for audit and site metrics reports.

    example
    Code:
    public BaseController extends MultiActionController{
           public void myAudit(HttpServletRequest request){
                    .... code to save my required header info 
                    ....like referrer,browser agent,etc to database
           }
    }
    now my extending class would be like this
    Code:
    public Controller1 extends BaseController{
            public ModelAndView vetsHandler(HttpServletRequest request,       HttpServletResponse response) throws ServletException {
                    //i can call myAudit(request) from here
    		return new ModelAndView("vetssView");
    	}	      
    }
    as you can see i can call myAudit from the method (programatically) if i want to, but is there a way to call myAudit function automatically whenever a method from the extending class of my BaseController is called. or am I asking too much.

    anyway, need suggestion from any expert out there.

    thanks.

  2. #2
    Join Date
    Sep 2005
    Location
    Newcastle, Australia
    Posts
    61

    Default

    how about using a servlet filter?

    Perhaps subclass org.springframework.web.filter.OncePerRequestFilte r

  3. #3

    Default

    i believe this will trap all request, i just need to log from my known controller, i.e. BaseController.

    is there any way around this?

    plus i am also thinking of implementing a simple session-based authentication temporarily while i'm still learning acegi.


    thanks for any help.

  4. #4

    Default

    Hi,

    I'd think about an empty (abstract?) method in the BaseController that is called by it after myAudit. All subcontrollers override this method like this:

    Code:
    public BaseController extends MultiActionController{
           public void myAudit(HttpServletRequest request){
                    .... code to save my required header info
                    ....like referrer,browser agent,etc to database
                    vetsHandler(request, ...)
           }
    
          abstract public <whateverTypeYouNeed> vetsHandler&#40;request, ...&#41; ...
    &#125; 
    
    public Controller1 extends BaseController&#123;
            public ModelAndView vetsHandler&#40;HttpServletRequest request,       HttpServletResponse response&#41; throws ServletException &#123;
                    
          return new ModelAndView&#40;"vetssView"&#41;;
       &#125;        
    &#125;
    Regards,
    Simon

  5. #5
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    Would a HandlerInterceptor meet your needs here?
    Chris Harris
    Carlisle, UK

  6. #6
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Alteratively, look into using AOP. This sounds like an ideal candidate.

    (shameless self promotion): AOP scared the c*** out of me until I actually knuckled down and did a small example: http://blogs.warwick.ac.uk/colinyate...ing_aop_rocks/

  7. #7

    Default

    i've look at HandlerInterceptor, based on the api documentation


    A HandlerInterceptor gets called before the appropriate HandlerAdapter triggers the execution of the handler itself. This mechanism can be used for a large field of preprocessing aspects, e.g. for authorization checks, or common handler behavior like locale or theme changes. Its main purpose is to allow for factoring out repetitive handler code.


    dont have any experiece using this, can someone show me how.

    because i would need the request object from that interceptor and
    especially how to assign this to a particular controller only, or any working example or implementation of handling this.

  8. #8
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    In your spring config, split your handler mappings into those that want the interceptor, and those that don't.
    Attatch your interceptor to the appropriate mapping:
    Code:
        <bean id="urlMapping3" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="order"><value>3</value></property>
            <property name="mappings">
                <props>
                    <prop key="/appcontrol.bit">appcontrolCtlr</prop>
                    <prop key="/upload.bit">uploadCtlr</prop>
                    <prop key="/cacheadmin.bit">cacheCtlr</prop>
                    <prop key="/paymenttest.bit">paymentTestCtlr</prop>
                </props>
            </property>
            <property name="interceptors">
                <list>
                    <ref bean="siteIdInterceptor"/>
                    <ref bean="secureInterceptor"/>
                    <ref bean="adminJobInterceptor"/>
                </list>
            </property>
        </bean>
    Then write your HandlerInterceptor, overriding preHandle and postHandle as required. Both these methods get passed the request (among other things).

    HTH
    Chris Harris
    Carlisle, UK

  9. #9

    Default

    thanks chris, i am confident i can proceed now.

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. PerformanceMonitorInterceptor
    By tnist in forum AOP
    Replies: 3
    Last Post: Aug 24th, 2005, 01:39 PM
  5. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •