springmvc自定义类型转换器实例如下:
一. 创建实体类
package pojo; public class GoodsModel { private String goodsname; private double goodsprice; private int goodsnumber; public String getGoodsname() { return goodsname; } public void setGoodsname(String goodsname) { this.goodsname = goodsname; } public double getGoodsprice() { return goodsprice; } public void setGoodsprice(double goodsprice) { this.goodsprice = goodsprice; } public int getGoodsnumber() { return goodsnumber; } public void setGoodsnumber(int goodsnumber) { this.goodsnumber = goodsnumber; } }
二. 创建控制器类
package controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import pojo.GoodsModel; @Controller @RequestMapping("/my") public class ConverterController { @RequestMapping("/converter") public String myConverter(@RequestParam("goods") GoodsModel gm,Model model) { model.addAttribute("goods",gm); return "showGoods"; } }
三. 创建自定义的转换器类
自定义类型转换器类需要实现 Converter 接口,重写 convert(S) 接口方法。
convert(S) 方法的功能是将源数据类型 S 转换成目标数据类型 T
package converter; import org.springframework.core.convert.converter.Converter; import pojo.GoodsModel; public class GoodsConverter implements Converter<String, GoodsModel> { public GoodsModel convert(String source) { // 创建一个Goods实例 GoodsModel goods = new GoodsModel(); // 以“,”分隔 String stringvalues[] = source.split(","); System.out.println(source); if (stringvalues != null && stringvalues.length == 3) { // 为Goods实例赋值 goods.setGoodsname(stringvalues[0]); goods.setGoodsprice(Double.parseDouble(stringvalues[1])); goods.setGoodsnumber(Integer.parseInt(stringvalues[2])); return goods; } else { throw new IllegalArgumentException(String.format( "类型转换失败, 需要格式'orange, 10.58,200 ',但格式是[% s ] ", source)); } } }
四. 注册类型转换器
WEB-INF 目录下创建配置文件 springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 使用扫描机制扫描控制器类,控制器类都在controller包及其子包下 --> <context:component-scan base-package="controller" /> <!-- annotation-driven用于简化开发的配置,注解DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --> <!-- 使用resources过滤掉不需要dispatcherservlet的资源(即静态资源,例如css、js、html、images)。 在使用resources时必须使用annotation-driven,否则resources元素会阻止任意控制器被调用 --> <!-- 允许css目录下的所有文件可见 --> <mvc:resources location="/css/" mapping="/css/**" /> <!-- 允许html目录下的所有文件可见 --> <mvc:resources location="/html/" mapping="/html/**" /> <!-- 允许css目录下的所有文件可见 --> <mvc:resources location="/images/" mapping="/images/**" /> <!--注册类型转换器GoodsConverter--> <bean id="goodsConverter" class="converter.GoodsConverter"></bean> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <ref bean="goodsConverter"/> </set> </property> </bean> <mvc:annotation-driven conversion-service="conversionService" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
五. 创建相关视图
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/my/converter" method= "post"> 请输入产品信息(格式为apple, 10.58,200): <input type="text" name="goods" /><br> <input type="submit" value="提交" /> </form> </body> </html>
应用的 /WEB-INF/view 目录下创建信息显示页面 showGoods.jsp
您创建的商品信息如下: <!-- 使用EL表达式取出model中的goods信息 --> 商品名称为:${goods.goodsname } 商品价格为:${goods.goodsprice } 商品名称为:${goods.goodsnumber }
七. 页面访问如下:
显示结果如下:
商品名称为:grape 商品价格为:12.8 商品名称为:300