场景
自己定义 "张三|26" 这个格式表示“用户名|年龄",提交到控制器后自动转换成用户对象,这时候需要实现自定义类型转换。
demo
配置
自定义转换的实现类 UserConvert.java
package com.shuoeasy.springmvc; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; /** * 自定义类型转换的实现类 * */ @Component public class UserConvert implements Converter<String, User> { @Override public User convert(String source) { System.out.println("自定义转换:" + source); // 输入字符串定义: 张三|26 if(source==null || "".equals(source)){ return null; } String [] arr = source.split("\\|"); if(arr==null || arr.length!=2){ return null; } User user = new User(); user.setUname(arr[0]); user.setAge(Integer.parseInt(arr[1])); return user; } }
springmvc.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: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-4.0.xsd 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"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.shuoeasy.springmvc"></context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/views/"></property> <!-- 文件名 --> <property name="suffix" value=".jsp"></property> </bean> <!-- 自定义转换 --> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <!-- 配置 ConversionService --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <ref bean="userConvert"/> </set> </property> </bean> </beans>
实体 User.java
package com.shuoeasy.springmvc; public class User { private int uid; private String uname; private int age; public User(){} public User(int uid, String uname, int age) { super(); this.uid = uid; this.uname = uname; this.age = age; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [uid=" + uid + ", uname=" + uname + ", age=" + age + "]"; } }
提交的表单:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <h2>Hello World !</h2> <form action="home/show" method="post"> <input type="text" name="data" value="张三|26"/> <input type="submit" value="提交" /> </form> <br/> </body> </html>
控制器:
package com.shuoeasy.springmvc; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @RequestMapping("/home") @Controller public class Home { /** * 获取字符串的"data"参数,转换成User对象 * 路径 : home/show */ @RequestMapping("/show") public ModelAndView showPage(@RequestParam("data") User user){ System.out.println("user=" + user); ModelAndView mav = new ModelAndView("home_index"); mav.addObject("user", user); return mav; } }
视图:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> 我是mvc页面! <br/> uname:${requestScope.user.uname } <br/> age:${requestScope.user.age } </body> </html>
页面截图: