SpringMVC转换器

mvc转换器可自定义转化器,比如将前台传入的字符串转换为自定义的对象

第一步:

package com.china.springmvc.curd.Converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import com.china.springmvc.curd.pojo.Department;
import com.china.springmvc.curd.pojo.Employee;


/**
 * 自定义转换器操作步骤
 * 1:转换器需要实现接口Converter<s,t> s表示需要转换的类型,t表示转换以后类型
 * 2:注解使用,加入ioc容器
 * 3:重写方法
 * 4:配置文件添加 <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
 *   <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
     <property name="converters">
       <list>
         <ref bean="beanConverter"/>
       </list>
     </property>
  </bean>
 */
@Component
public class BeanConverter implements Converter<String, Employee> {


@Override
public Employee convert(String source) {

if(source !=null){
String[] split = source.split("-");
if(split.length ==4 &&split!=null){
String lastName=split[0];
String email=split[1];

Integer gender=Integer.parseInt(split[2]);

Department department =new Department();
department.setId(Integer.parseInt(split[3]));
Employee emp=new  Employee(null, lastName, email, gender, department);
return emp;
}

}
return null;
}
}

第二步:注册转换器

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

   <context:component-scan base-package="com.china.springmvc.curd"></context:component-scan>
  
              <bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/views/"/>
     <property name="suffix" value=".jsp"/>
  </bean>
  
  <!-- 解决springmvc中静态资源 ,自动检测请求是否映射,如果有则返回对应信息,否则寻找对应的静态资源
    如服务器默认的servlet不是叫default需要显示的指定servlet的名称:静态资源由服务器的servlet处理,非静态请求由mvc处理
  -->
  <mvc:default-servlet-handler  default-servlet-name="default"/>




  <!-- 已下注解是标配,会自动注册requestMaping的转化器等 -->
  <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
  
  <!-- FormattingConversionServiceFactoryBean可以自定义转换器,并且支持mvc提供数据类型格式化功能 -->  
  <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
     <property name="converters">
       <list>
         <ref bean="beanConverter"/>
       </list>
     </property>
  </bean>
</beans>

第三步:附加原理解释信息







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值