springmvc配置converter转换器----去除传递参数中的空格

springmvc配置传递参数去除前后空格

1. 创建自定义转换器类
package cn.jay.common.conversion;

import org.springframework.core.convert.converter.Converter;

/**
 * 自定义转换器
 * 去掉前后空格
 * <S, T>  : S  页面上类型   T : 转换后的类型
 * @author lx
 *
 */
public class CustomConverter implements Converter<String, String>{
    //去掉前后空格
    @Override
    public String convert(String source) {
        // TODO Auto-generated method stub
        try {
            if(null != source){
                source = source.trim();
                if(!"".equals(source)){
                    return source;
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

}
2. 在springmvc文件中进行配置
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://code.alibabatech.com/schema/dubbo        
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <!-- 扫描 @Contr -->  
    <context:component-scan base-package="cn.jay" />

    <!-- 处理器 映射器  适配器 -->
    <mvc:annotation-driven conversion-service="conversionService"/>

    <!-- 视图解释器 jsp -->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/login/"/>
        <property name="suffix" value=".jsp"/>
    </bean>


    <!-- Converter转换器 工厂  注: 需要在 视图适配器 中进行配置-->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!-- 日期 -->
        <!-- 去掉前后空格 -->
        <property name="converters">
            <list>
                <bean class="cn.jay.common.conversion.CustomConverter"></bean>
            </list>
        </property>
    </bean>

    <!-- 图片上传  配置springmvc支持图片上传-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>


    <import resource="dubbo-consumer.xml"/>


</beans>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值