springmvc自定義類型轉(zhuǎn)換器實例如下:
一. 創(chuàng)建實體類
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;
}
}二. 創(chuàng)建控制器類
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";
}
}三. 創(chuàng)建自定義的轉(zhuǎn)換器類
自定義類型轉(zhuǎn)換器類需要實現(xiàn) Converter 接口,重寫 convert(S) 接口方法。
convert(S) 方法的功能是將源數(shù)據(jù)類型 S 轉(zhuǎn)換成目標數(shù)據(jù)類型 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) {
// 創(chuàng)建一個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(
"類型轉(zhuǎn)換失敗, 需要格式'orange, 10.58,200 ',但格式是[% s ] ", source));
}
}
}四. 注冊類型轉(zhuǎn)換器
WEB-INF 目錄下創(chuàng)建配置文件 springmvc-servlet.xml,并在配置文件中注冊自定義類型轉(zhuǎn)換器,配置文件代碼如下
<?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用于簡化開發(fā)的配置,注解DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --> <!-- 使用resources過濾掉不需要dispatcherservlet的資源(即靜態(tài)資源,例如css、js、html、images)。 在使用resources時必須使用annotation-driven,否則resources元素會阻止任意控制器被調(diào)用 --> <!-- 允許css目錄下的所有文件可見 --> <mvc:resources location="/css/" mapping="/css/**" /> <!-- 允許html目錄下的所有文件可見 --> <mvc:resources location="/html/" mapping="/html/**" /> <!-- 允許css目錄下的所有文件可見 --> <mvc:resources location="/images/" mapping="/images/**" /> <!--注冊類型轉(zhuǎn)換器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>
五. 創(chuàng)建相關(guān)視圖
<%@ 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">
請輸入產(chǎn)品信息(格式為apple, 10.58,200):
<input type="text" name="goods" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>應(yīng)用的 /WEB-INF/view 目錄下創(chuàng)建信息顯示頁面 showGoods.jsp
您創(chuàng)建的商品信息如下:
<!-- 使用EL表達式取出model中的goods信息 -->
商品名稱為:${goods.goodsname }
商品價格為:${goods.goodsprice }
商品名稱為:${goods.goodsnumber }七. 頁面訪問如下:

顯示結(jié)果如下:
商品名稱為:grape 商品價格為:12.8 商品名稱為:300
