Spring Framework
  1. Spring Framework
  2. SPR-6817

Portlet MVC - <mvc:annotation-driven /> doesn't inject conversion service and validator in WebDataBinder

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 3.0 GA
    • Fix Version/s: General Backlog
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      I have a Spring MVC portlet with one spring configuration file:

      <?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="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      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">

      <context:component-scan
      base-package="com.cedac.liferay.calcoli.portlets.ammortamenti" />

      <mvc:annotation-driven conversion-service="conversionService" validator="validatorJSR303"/>

      <!-- VIEW RESOLUTION AND RENDERING -->

      <bean id="viewResolver"
      class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
      <property name="basename" value="views"/>
      </bean>

      <!-- Message Bundle -->

      <!-- Message source for this context, loaded from localized "messages_xx" files -->
      <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
      <property name="basenames">
      <list>
      <value>messages</value>
      <value>com.cedac.liferay.calcoli.portlets.ammortamenti.messages</value>
      </list>
      </property>
      </bean>

      </beans>

      and a global spring mvc configuration file:

      <?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:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns="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/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">

      <!--
      Conversion service
      Supporta anche le annotazioni per la formattazione
      -->
      <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">

      </bean>

      <!-- Validation JSR 303 -->
      <bean id="validatorJSR303" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

      </beans>

      both of them are correctly loaded but when I debug the above method in the controller, conversion service and validator are null.

      @InitBinder
      public void initBinder(WebDataBinder binder) {

      }

      I have to autowire the beans and set them into the binder:

      @Autowired
      private AmmortamentiService ammortamentiService;

      @Autowired
      private Validator validator;

      @InitBinder
      public void initBinder(WebDataBinder binder)

      { //Conversion Service binder.setConversionService(conversionService); //Validator binder.setValidator(ammortamentoValidator); }

        Issue Links

          Activity

          Hide
          Juergen Hoeller added a comment -

          I'm afraid that this is a fundamental limitation in Spring 3.0: The MVC namespace works in a Servlet MVC environment only, i.e. within a DispatcherServlet. It has no effect in a Portlet environment. I've added a corresponding warning to the namespace docs.

          We'll revisit this for Spring 3.1. For the time being, in Spring 3.0, you'll have to configure an AnnotationMethodHandlerAdapter bean, passing a ConfigurableWebBindingInitializer into its "webBindingInitializer" property. That nested ConfigurableWebBindingInitializer bean in turn features "conversionService" and "validator" properties.

          Juergen

          Show
          Juergen Hoeller added a comment - I'm afraid that this is a fundamental limitation in Spring 3.0: The MVC namespace works in a Servlet MVC environment only, i.e. within a DispatcherServlet. It has no effect in a Portlet environment. I've added a corresponding warning to the namespace docs. We'll revisit this for Spring 3.1. For the time being, in Spring 3.0, you'll have to configure an AnnotationMethodHandlerAdapter bean, passing a ConfigurableWebBindingInitializer into its "webBindingInitializer" property. That nested ConfigurableWebBindingInitializer bean in turn features "conversionService" and "validator" properties. Juergen
          Hide
          whardwick added a comment -

          It would be nice to see this revisited now that 3.1 has been released.

          Show
          whardwick added a comment - It would be nice to see this revisited now that 3.1 has been released.
          Hide
          Tang Jianyu added a comment -

          I still need to use the workaround in version 3.1.2, voted for fixing

          Show
          Tang Jianyu added a comment - I still need to use the workaround in version 3.1.2, voted for fixing

            People

            • Assignee:
              Juergen Hoeller
              Reporter:
              Alex Landini
              Last updater:
              Sébastien Deleuze
            • Votes:
              13 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since last comment:
                1 year, 13 weeks, 1 day ago