Announcement Announcement Module
Collapse
No announcement yet.
Locale Interceptor never called Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Locale Interceptor never called

    Hi Forum!

    I've got a little Problem. I defined the following:
    PHP Code:
    <context:component-scan base-package="fhkoeln.edb.nftool" use-default-filters="false">

    <
    mvc:annotation-driven/>

    <
    mvc:interceptors>
            <
    bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
            <
    bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/>
            <
    bean class="fhkoeln.edb.nftool.web.TestInterceptor" />
    </
    mvc:interceptors
    The Behavior: I set a breakpoint in the LocaleChangeInterceptor and in my TestInterceptor, the TestInterceptor even contains logging output. But nothing of this is ever called.

    My URL is like localhost:8080/3nftool/app/doexercise/?lang=en&execution=e2s3

    The Project was initially generated by Roo. It uses Tiles 2 (Is that important?).

    The result is, that I can't change the locale. First, I thought, the <mvc:interceptors> doesn't affect the URL-Handler-Mapping in conjunction with tiles ... but it seems to work in others projects.

    By the way: Yes, I started Apache 7 in debugging mode. ;-)

    My TestInterceptor:
    PHP Code:
    public class TestInterceptor extends HandlerInterceptorAdapter {
        
        
    Logger logger Logger.getLogger(TestInterceptor.class);

        @
    Override
        
    public boolean preHandle(HttpServletRequest request,
                
    HttpServletResponse responseObject handlerthrows Exception {
            
    logger.warn("### Ich werde aufgerufen!");
            return 
    super.preHandle(requestresponsehandler);
        }



  • #2
    Ok, I could set an breakpoint at SessionLocaleResolver ... and on Line 56, which contains the following:
    PHP Code:
    Locale locale = (LocaleWebUtils.getSessionAttribute(requestLOCALE_SESSION_ATTRIBUTE_NAME); 
    the result of locale is null. Seems, it could not get the session param?!?

    Another strange thing: The Eclipse debugger says, LOCALE_SESSION_ATTRIBUTE_NAME = "org.springframework.web.servlet.i18n.SessionLocal eResolver.LOCALE"

    And this does not explain, why my interceptor isn't called ... and setLocale() isn't called, too!!
    Last edited by sja; Aug 24th, 2010, 05:23 PM.

    Comment


    • #3
      Because you use struts, and struts doesn't pass (in general) through the dispatcher servlet and hence nothing is called or being set. It only works if, instead of the Struts ActionServlet/-Filter you let it pass through the DispatcherServlet.

      Comment


      • #4
        Thank you, Marten, for your answer, but, I don't use struts! Just plain JSP views.

        Comment


        • #5
          Sorry I should read better. I read struts where you actually said tiles .

          The interceptors in this case only apply to the AnnotationHandlerMapping and not to the url based handlermappings. You will have to add them seperatly to that handlermapping, or move the url configuration to also annotations based...

          Comment


          • #6
            Hmm, I think I know what you meant ... I've no SimpleUrlHandlerMapping bean. I've got an AnnotationMethodHandlerAdapter ....

            it's simple and faste, if I post my whole XML:

            HTML Code:
            <?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns="http://www.springframework.org/schema/beans"
            	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:webflow="http://www.springframework.org/schema/webflow-config"
            	xmlns:p="http://www.springframework.org/schema/p"
            	xsi:schemaLocation="
                       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                       http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">
            
            	<!--Maps request paths to flows in the flowRegistry-->
            	<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
            		<property name="order" value="0" />
            		<property name="flowRegistry" ref="flowRegistry" />
            	</bean>
            
            	<!-- Dispatches requests mapped to flows to FlowHandler implementations -->
            	<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
            		<property name="flowExecutor" ref="flowExecutor" />
            	</bean>
            
            	<!--Dispatches requests mapped to POJO @Controllers implementations-->
            	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
            
            	<!-- Dispatches requests mapped to org.springframework.web.servlet.mvc.Controller implementations -->
            	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
            	
            	<!-- Executes flows: the entry point into the Spring Web Flow system -->
            	<webflow:flow-executor id="flowExecutor" ></webflow:flow-executor>
            
            	<!-- The registry of executable flow definitions -->
            	<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF/views">
            		<webflow:flow-location-pattern value="/**/*-flow.xml" />
            	</webflow:flow-registry>
            
            	<!-- Plugs in a custom creator for Web Flow views -->
            	<webflow:flow-builder-services id="flowBuilderServices"	view-factory-creator="mvcViewFactoryCreator" development="true" />
            	
            	<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
            		<property name="viewResolvers" ref="tilesViewResolver" />
            		<property name="useSpringBeanBinding" value="true"/>
            	</bean>
            </beans>
            HTML Code:
            <?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
            				
            	<!-- The controllers are autodetected POJOs labeled with the @Controller annotation.
            -->
            	<context:component-scan base-package="fhkoeln.edb.nftool" use-default-filters="false">
            		<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
            	</context:component-scan>
            	
            	<!-- Turns on support for mapping requests to Spring MVC @Controller methods
            	     Also registers default Formatters and Validators for use across all @Controllers
            -->
            	<mvc:annotation-driven/>
            	
            	<!-- register "global" interceptor beans to apply to all registered HandlerMappings
            -->
            	<mvc:interceptors>
            <!--		<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>-->
            		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/>
            		<bean class="fhkoeln.edb.nftool.web.validation.TestInterceptor" />
            	</mvc:interceptors>
            	
            	<!-- selects a static view for rendering without the need for an explicit controller
            -->
            	<mvc:view-controller path="/index"/>
            	<mvc:view-controller path="/uncaughtException"/>
            	<mvc:view-controller path="/resourceNotFound"/>
            	<mvc:view-controller path="/dataAccessFailure"/>
            
            	<!-- Resolves logical view names returned by Controllers to Tiles; a view
            		 name to resolve is treated as the name of a tiles definition
            -->
            	<bean class="org.springframework.js.ajax.AjaxUrlBasedViewResolver" id="tilesViewResolver">
            		<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
            	</bean>
            
            	<!-- Configures the Tiles layout system -->
            	<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
            		<property name="definitions">
            			<list>
            				<value>/WEB-INF/layouts/layouts.xml</value>
            				<!-- Scan views directory for Tiles configurations -->
            				<value>/WEB-INF/views/**/views.xml</value>
            			</list>
            		</property>
            	</bean>
            
            	<!-- Resolves localized messages*.properties and application.properties files in the application to	allow for internationalization. 
            		The messages*.properties files translate Roo generated messages which are part of the admin interface, the application.properties
            		resource bundle localizes all application specific messages such as entity names and menu items.
            -->
            	<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application" p:fallbackToSystemLocale="false"/>
            	
            	<!-- store preferred language configuration in a cookie -->
            <!--	<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver" p:cookieName="locale"/>	-->
            	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
            	
            	<!-- resolves localized <theme_name>.properties files in the classpath to allow for theme support
            -->
            	<bean class="org.springframework.ui.context.support.ResourceBundleThemeSource" id="themeSource"/>
            	
            	<!-- store preferred theme configuration in a cookie -->
            	<bean class="org.springframework.web.servlet.theme.FixedThemeResolver" id="themeResolver" p:defaultThemeName="edb"/>
            
            	<!-- This bean resolves specific types of exceptions to corresponding logical - view names for error views. 
            	     The default behaviour of DispatcherServlet - is to propagate all exceptions to the servlet container: 
            	     this will happen - here with all other types of exceptions.
            -->
            	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:defaultErrorView="uncaughtException">
            		<property name="exceptionMappings">
            			<props>
            				<prop key=".DataAccessException">dataAccessFailure</prop>
            				<prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</prop>
            				<prop key=".TypeMismatchException">resourceNotFound</prop>
            				<prop key=".MissingServletRequestParameterException">resourceNotFound</prop>
            			</props>
            		</property>
            	</bean>
            	
            	<!-- allows for integration of file upload functionality -->
            	<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"/>
            	
            	<context:property-placeholder location="WEB-INF/i18n/application.properties"/>
            <import resource="webflow-config.xml"/>
            </beans>
            Thats all. All the other stuff of my project are some POJOs which represent the entities and two validators. Ah: End my flow-xml.

            Where do I have to inject my interceptors?

            Comment


            • #7
              I had a similar issue today(only with the "localeChangeInterceptor" and I resolved it by adding it into the FlowHandlerMapping :

              Code:
              <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
              		<property name="order" value="0"/>
              		<property name="flowRegistry" ref="flowRegistry" />
                      <property name="interceptors">
                          <list>
                              <ref bean="localeChangeInterceptor"/>
                          </list>
                      </property>
              	</bean>

              after adding the localeChangeInterceptor bean :
              Code:
              	<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
              It works fine but I would love to find a way of making it work (in the flow) keeping "<mvc:interceptors", though...
              Last edited by boss94; Sep 27th, 2010, 01:42 PM.

              Comment


              • #8
                If you register your own HandlerMappings you will also need to register the interceptors to those handlermappings. The namespace will not help you there, next to that the FlowHandlerMapping is a bit of a special case as it doesn't really react to urls (whereas the mvc:interceptors do).

                Just curious is everything loaded in the same applicationcontext? Loading the HandlerAdapters in the root context (or a different servlet) then the mvc:interceptors isn't going to work.

                Comment


                • #9
                  Originally posted by Marten Deinum View Post
                  If you register your own HandlerMappings you will also need to register the interceptors to those handlermappings. The namespace will not help you there, next to that the FlowHandlerMapping is a bit of a special case as it doesn't really react to urls (whereas the mvc:interceptors do).

                  Just curious is everything loaded in the same applicationcontext? Loading the HandlerAdapters in the root context (or a different servlet) then the mvc:interceptors isn't going to work.
                  In my case, I did reference the interceptor in my url handlermapping as following:

                  Code:
                  <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                      <property name="interceptors">
                          <list>
                              <ref bean="localeChangeInterceptor"/>
                          </list>
                      </property>
                  </bean>
                  but then I was so keen on using the mvc namespace that I did a quite ugly piece of code :
                  Code:
                  	<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
                  	<mvc:interceptors>
                  	    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
                  	</mvc:interceptors>
                  the first bean being referenced in the flowHandlerMapping only.

                  But both worked (it's picking the corresponding messageSource, the only issue being that ${pagecontext.response.locale} isn't changing...)

                  Do you know any way of changing the locale in the flow(using interceptors) without doing all this? Because I tried to do it with the solely "<mvc:interceptor" and it didn't work...

                  Comment

                  Working...
                  X