Announcement Announcement Module
Collapse
No announcement yet.
How to configure an interceptor in Spring MVC Portlet? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to configure an interceptor in Spring MVC Portlet?

    Hi all,

    i've been looking at documentation on how to configure custom interceptor in Spring Portlet MVC. The documentation states that it should be the same as in Spring MVC :

    Code:
     <bean id="handlerMapping"
              class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
            <property name="interceptors">
                <list>
                    <ref bean="officeHoursInterceptor"/>
                </list>
            </property>
        </bean>
    
        <bean id="officeHoursInterceptor"
              class="samples.TimeBasedAccessInterceptor">
            <property name="openingTime" value="9"/>
            <property name="closingTime" value="18"/>
        </bean>
    So my application context looks like this:

    Code:
        <mvc:annotation-driven validator="validator" />
    
        <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
           
        <bean id="conversationInterceptor" class="my.namespace.ConversationInterceptor"/>
        <bean id="handlerMapping" class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping">
             <property name="interceptors">
                 <list>
                    <ref bean="conversationInterceptor" />
                </list>
            </property>
        </bean>
    
        <bean id="annotationMethodHandlerAdapter" class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="webBindingInitializer">
                <bean id="configurableWebBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
                    <property name="validator">
                        <ref bean="validator" />
                    </property>
                </bean>
            </property>
            <property name="sessionAttributeStore">
                <bean class="my.namespace.ConversationAwareSessionAttributeStore">
                </bean>
            </property>
        </bean>
    
        <context:annotation-config />
        <context:component-scan base-package="my.namespace.custom" />
       
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp" />
            <property name="contentType" value="text/html;charset=UTF-8" />
        </bean>

    And i've wrote a simple dummy interceptor:

    Code:
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;
    
    import org.springframework.web.portlet.handler.HandlerInterceptorAdapter;
    
    public class ConversationInterceptor extends HandlerInterceptorAdapter {
    
    
        @Override
        public boolean preHandleRender(final RenderRequest request, final RenderResponse response,
                final Object handler)
                throws Exception {
            handleConversationId(request);
            return super.preHandleRender(request, response, handler);
        }
    
        @Override
        public boolean preHandleAction(final ActionRequest request, final ActionResponse response,
                final Object handler)
                throws Exception {
            handleConversationId(request);
            return super.preHandleAction(request, response, handler);
        }
    }

    But it does not work:

    Code:
    ERROR DispatcherPortlet:559 - Could not complete request
    org.springframework.web.portlet.NoHandlerFoundException: No handler found for portlet request: mode 'view', phase 'RENDER_PHASE', parameters map[[empty]]
    	at org.springframework.web.portlet.DispatcherPortlet.noHandlerFound(DispatcherPortlet.java:997)
    	at org.springframework.web.portlet.DispatcherPortlet.doRenderService(DispatcherPortlet.java:700)
    It seems that i am overriding the default handler mappings, and the default ones get lost. But what are the default handlers? Should i somehow include ParameterMappingInterceptor ?

  • #2
    Ok, it seems that explicitly defining my controllers does the trick, eg if i include lines:

    Code:
       
      <bean class="my.namespace.controllers.FirstController" />
      <bean class="my.namespace.controllers.SecondController" />
    it start to work as before. But the question is - why do i need to define my controllers as beans in config when i define org.springframework.web.portlet.mvc.annotation.Def aultAnnotationHandlerMapping in config?

    If i remove the org.springframework.web.portlet.mvc.annotation.Def aultAnnotationHandlerMapping config part, controllers work without defining them in config.

    Comment

    Working...
    X