How to do spring request parameter conversion

后端 未结 5 1528
南旧
南旧 2021-02-05 13:36

In a Spring 3 based web (portlet) application I have a controller with a method like this:

@RenderMapping
public ModelAn         


        
相关标签:
5条回答
  • 2021-02-05 14:00

    Spring Portlet MVC 3.0 does not support

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

    Visit https://jira.springsource.org/browse/SPR-6817 for more info about this.

    However you can add this to your common applicationContext

    <bean
        class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="webBindingInitializer">
            <bean
                class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
                <property name="conversionService">
                    <list>
                        <ref bean="conversionService" />
                    </list>
                </property>
            </bean>
        </property>
    </bean>
    

    This way you do not need add @InitBinder to every single controller

    and of course

    <bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <!-- converter implementations here -->
            </list>
        </property>
    </bean>
    
    0 讨论(0)
  • 2021-02-05 14:05

    Implement a WebArgumentResolver:

    public class MyArgumentResolver implements WebArgumentResolver
    {
        @Override
        public Object resolveArgument(MethodParameter methodParameter,
                NativeWebRequest webRequest) throws Exception
        {
            Class<?> paramType = methodParameter.getParameterType();
            if (paramType == MyClass.class)
            {
                String parameterName = methodParameter.getParameterName();
                String stringParameter = webRequest.getParameter(parameterName);
                return convert(stringParameter);
            }
            return UNRESOLVED;
        }
    }
    

    And register it in your applicationContext.xml:

    <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="customArgumentResolver">
            <bean class="com.dshs.eakte.util.MyArgumentResolver" />
        </property>
    </bean>
    

    This works and even has the advantage of allowing parameter conversion that is based on multiple method parameters.

    0 讨论(0)
  • 2021-02-05 14:06

    i think you need to use something like

    public ModelAndView handleRenderRequest(...,@ModelAttribute("myObject") MyClass myObject)
    
    0 讨论(0)
  • 2021-02-05 14:08

    You are correct that Converter (and ConverterFactory) are the successors to property editors. Your problem may be that you are not accepting the appropriate type as a parameter to your converter, but that's hard to say without seeing the converter code. If you are expecting Long or Integer you may actually be getting a String from Spring and need to perform that key conversion yourself first.

    As for configuration, I believe you need to list all of your converters in the bean configuration in your xml. If you annotate your converter implementation with @Component you might be able to reference it by the bean name instead of the fully qualified path, but I have only tried that for a ConverterFactory, not a Converter.

    Finally, on specific converters, it looks like you may be able to configure the conversion service at the controller level (see Javi's answer on Setting up a mixed configuration for annotation-based Spring MVC controllers ) and then you could just place that method (and others that require that controller) into a controller that uses a secondary conversion service which you ought to be able to inject by name with the @Resource annotation.

    0 讨论(0)
  • 2021-02-05 14:10

    To achieve something similar to what you're doing, I found this blog entry useful.

    0 讨论(0)
提交回复
热议问题