应用场景
比如我在界面上,输入一个字符串,里面的内容包括了一个实体类的所有信息,但是我只提供一个输入框,这样我们就可以用到springmvc的自定义类型转换器
所以我们可以自定义一个转换器,实现Converter接口
package com.asiainfo.springmvc.conversion;
import org.apache.log4j.Logger;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import com.asiainfo.springmvc.controller.BaseController;
import com.asiainfo.springmvc.pojo.User;
@Component
public class DefineConversion implements Converter<String, User>{
private static final Logger logger = Logger.getLogger(BaseController.class);
public User convert(String source) {
User user = new User();
user.setUsername(source);
logger.debug("测试converter============"+source);
return user;
}
}
配置spring的xml文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.asiainfo.springmvc" />
<!-- 静态资源的访问配置 -->
<mvc:resources location="/bootstrap/**" mapping="/bootstrap/**"/>
<mvc:resources location="/jquery/**" mapping="/jquery/**"/>
<!-- 视图解释类 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 国际化配置文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
<!-- 视图控制器,这里配置的是指不需要经过handler,直接通过请求到页面,这里配置以后
必须配置<mvc:annotation-driven/>,不然通过handler的其他请求就会报404
-->
<mvc:view-controller path="/success" view-name="success"/>
<!-- 配置类型转换 -->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="defineConversion"/>
</set>
</property>
</bean>
<!-- 这里必须conversion-service="conversionService",不然会找不到转换器 -->
<mvc:annotation-driven conversion-service="conversionService"/>
</beans>
controller层:
package com.asiainfo.springmvc.controller;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.asiainfo.springmvc.pojo.User;
@RequestMapping("/conversion")
@Controller
public class ConversionController {
private static final Logger logger = Logger.getLogger(BaseController.class);
@RequestMapping("/testConversion")
public String testConversionConvert(@RequestParam("user")User user){
logger.debug("测试类型转换"+user);
return "success";
}
}
jsp:
<a href="base/testmodelAndView?username=1">test modelAndView</a>
测试结果:
DEBUG [http-bio-8888-exec-4] - 测试converter============zengml
DEBUG [http-bio-8888-exec-4] - 测试类型转换User [id=0, username=zengml, password=null, email=null]