SpringMVC 中 数据绑定 数据转换 自定义数据转换器

这里写图片描述

input.jsp页面加入这样的方式,将一个字符串提交到后台。

后台用SpringMVCTest.java处理它。但是这个处理是将字符串转换成对象,所以我们得去配置EmployeeConverter自定义的对象。

package com.hust.springmvc.test;

import com.hust.springmvc.dao.EmployDao;
import com.hust.springmvc.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by yexx on 16-11-11.
 */

@Controller
public class SpringMVCTest {

    @Autowired
    private EmployDao employDao;

    @RequestMapping(value = "/testConversionServiceConverter", method = RequestMethod.POST)
    public String testConverter(@RequestParam("employee") Employee employee) {
        System.out.print("save:" + employee);
        employDao.save(employee);
        return "redirect:/emps";
    }
}

EmployeeConverter.java

package com.hust.springmvc.converters;

import com.hust.springmvc.dao.DepartmentDao;
import com.hust.springmvc.entities.Department;
import com.hust.springmvc.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * Created by yexx on 16-11-11.
 */

@Component
public class EmployeeConverter implements Converter<String, Employee> {

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public Employee convert(String s) {
        if (s != null) {
            String[] vals = s.split("-");
            if (vals != null && vals.length == 4) {
                String lastName = vals[0];
                String email = vals[1];
                Integer gender = Integer.parseInt(vals[2]);

                Employee employee = new Employee(null, lastName, email, gender, departmentDao.getDepartment(Integer.parseInt(vals[3])));
                System.out.println(s + "--convert--" + employee);
                return employee;
            }
        }
        return null;
    }
}

在SpringMVC的配置文件配置如下。配置conversionService,而且还要在mvc:annotation-driven配置一下conversion-service。

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

    <!-- 配置 ConversionService -->
    <bean id="conversionService"
          class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="employeeConverter"/>
            </set>
        </property>
    </bean>

这样就能进行自定义数据转换器的功能了,会按照自定义的字符串的格式转换为相应的对象。

跟踪流程:

先在字符串进来的地方打断点,debug模式进入,找到resolveArgument,找到binder。

这里写图片描述

找到conversionService

这里写图片描述

找到最底下我们自己自定义的数据转换器,看到了包名了吧,还有String类型的。可以把他们加到watches,看到具体情况。

这里写图片描述

运行截图

这里写图片描述

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值