zoukankan      html  css  js  c++  java
  • SpringMVC第四篇【参数绑定详讲、默认支持参数类型、自定义参数绑定、RequestParam注解】

    参数绑定

    我们在Controller使用方法参数接收值,就是把web端的值给接收到Controller中处理,这个过程就叫做参数绑定

    默认支持的参数类型

    从上面的用法我们可以发现,我们可以使用request对象、Model对象等等,其实是不是可以随便把参数写上去都行???其实并不是的…

    Controller方法默认支持的参数类型有4个,这4个足以支撑我们的日常开发了

    • HttpServletRequest
    • HttpServletResponse
    • HttpSession
    • Model

    参数的绑定过程

    一般地,我们要用到自定义的参数绑定就是上面所讲的日期类型转换以及一些特殊的需求….对于平常的参数绑定,我们是无需使用转换器的,SpringMVC就已经帮我们干了这个活了…

    这里写图片描述

    自定义绑定参数【版本一】

    在上一篇我们已经简单介绍了怎么把字符串转换成日期类型了【使用的是WebDataBinder方式】…其实那是一个比较老的方法,我们可以使用SpringMVC更推荐的方式…

    在上次把字符串转换成日期类型,如果使用的是WebDataBinder方式的话,那么该转换仅仅只能在当前Controller使用…如果想要全部的Controller都能够使用,那么我们可以使用WebBindingInitializer方式

    如果想多个controller需要共同注册相同的属性编辑器,可以实现PropertyEditorRegistrar接口,并注入webBindingInitializer中。

    实现接口

    
    public class CustomPropertyEditor implements PropertyEditorRegistrar {
    
        @Override
        public void registerCustomEditors(PropertyEditorRegistry binder) {
            binder.registerCustomEditor(Date.class, new CustomDateEditor(
                    new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"), true));
    
        }
    
    }

    配置转换器

    注入到webBindingInitializer中

        <!-- 注册属性编辑器 -->
        <bean id="customPropertyEditor" class="cn.itcast.ssm.controller.propertyeditor.CustomPropertyEditor"></bean>
    
    
        <!-- 自定义webBinder -->
        <bean id="customBinder"
            class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
    
            <!-- propertyEditorRegistrars用于属性编辑器 -->
             <property name="propertyEditorRegistrars">
                <list>
                    <ref bean="customPropertyEditor" />
                </list>
            </property>
        </bean>
    
    
        <!-- 注解适配器 -->
        <bean
            class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <!-- 在webBindingInitializer中注入自定义属性编辑器、自定义转换器 -->
            <property name="webBindingInitializer" ref="customBinder"></property>
        </bean>
    
    

    自定义参数转换器【版本二】

    上面的方式是对象较老的,现在我们一般都是实现Converter接口来实现自定义参数转换…我们就来看看实现Converter比上面有什么好

    配置日期转换器

    
    public class CustomDateConverter implements Converter<String, Date> {
    
        @Override
        public Date convert(String source) {
            try {
                //进行日期转换
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(source);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
    }

    配置去除字符串转换器

    
    public class StringTrimConverter implements Converter<String, String> {
    
        @Override
        public String convert(String source) {
            try {
                //去掉字符串两边空格,如果去除后为空设置为null
                if(source!=null){
                    source = source.trim();
                    if(source.equals("")){
                        return null;
                    }
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return source;
        }
    }
    

    从上面可以得出,我们想要转换什么内容,就直接实现接口,该接口又是支持泛型的,阅读起来就非常方便了…

    配置转换器

    
    
        <!-- 转换器 -->
        <bean id="conversionService"
              class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
            <property name="converters">
                <list>
                    <bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
                    <bean class="cn.itcast.ssm.controller.converter.StringTrimConverter"/>
                </list>
            </property>
        </bean>
    
    
        <!-- 自定义webBinder -->
        <bean id="customBinder"
            class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <!-- 使用converter进行参数转 -->
            <property name="conversionService" ref="conversionService" />
        </bean>
    
    
        <!-- 注解适配器 -->
        <bean
            class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <!-- 在webBindingInitializer中注入自定义属性编辑器、自定义转换器 -->
            <property name="webBindingInitializer" ref="customBinder"></property>
        </bean>
    
    
    

    如果是基于<mvc:annotation-driven>的话,我们是这样配置的

    
    <mvc:annotation-driven conversion-service="conversionService">
    </mvc:annotation-driven>
    <!-- conversionService -->
        <bean id="conversionService"
            class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
            <!-- 转换器 -->
            <property name="converters">
                <list>
                    <bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
                    <bean class="cn.itcast.ssm.controller.converter.StringTrimConverter"/>
                </list>
            </property>
        </bean>

    @RequestParam注解

    我们一般使用的参数绑定都有遵循的规则:方法参数名要与传递过来的name属性名相同。

    在默认的情况下,只有名字相同,SpringMVC才会帮我们进行参数绑定…

    如果我们使用@RequestParam注解的话,我们就可以使方法参数名与传递过来的name属性名不同…

    该注解有三个变量

    • value【指定name属性的名称是什么】
    • required【是否必须要有该参数】
    • defaultvalue设置默认值

    例子:我们的方法参数叫id,而页面带过来的name属性名字叫item_id,一定需要该参数

    
    public String editItem(@RequestParam(value="item_id",required=true) String id) {
    
    }

  • 相关阅读:
    使用Dictionary键值对判断字符串中字符出现次数
    Linq实现字符串拼接多条件查询
    js数据类型转换
    js前端数据类型检测typeof,instanceof,Object.prototype.toString.call
    moment.js格式化日期,获取前一个月的时间
    css 样式中height100%失效问题
    记一次react项目运行崩溃
    null和undefined区别
    windows腾讯云/阿里云服务器更换操作系统为linux
    csrf攻击原理和防御-生成token防御代码
  • 原文地址:https://www.cnblogs.com/zhong-fucheng/p/7554356.html
Copyright © 2011-2022 走看看