SpringMVC自定义类型转化器

2024-11-02 06:13:23

1、ConversionService是Spring类型转换体系的核心接口。可以利用ConversionServiceFactoryBean在Spring的IOC容器中定义一个ConversionService。

SpringMVC自定义类型转化器

3、在SpringMVC控制层实现保存方法:package com.gwolf.spr坡纠课柩ingmvc.handlers;i罪焐芡拂mport java.util.Map;import javax.validation.Valid;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.validation.Errors;import org.springframework.validation.FieldError;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.gwolf.springmvc.dao.DepartmentDao;import com.gwolf.springmvc.dao.EmployeeDao;import com.gwolf.springmvc.domain.Employee;@Controllerpublic class EmployeeHandler { @Autowired private EmployeeDao employeeDao; @Autowired private DepartmentDao departmentDao; @RequestMapping("/testConversionServiceConverter") public String testConverter(@RequestParam("employee") Employee employee) { System.out.println("save:" + employee); employeeDao.save(employee); return "emps"; }}

SpringMVC自定义类型转化器

5、在SpringMVC配置文件中注册上面声明转化器:<?xml version屏顿幂垂="1.0" enco颊俄岿髭ding="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/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.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.gwolf.springmvc"> </context:component-scan> <!-- 配置视图解析器,如何把handler方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="100"></property> </bean> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/springmvc/helloworld"></mvc:mapping> <bean class="com.gwolf.springmvc.interceptors.SecondInterceptor"></bean> </mvc:interceptor> <bean class="com.gwolf.springmvc.interceptors.FirstInterceptor"></bean> </mvc:interceptors> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="i18n"></property> </bean> <mvc:view-controller path="/success" view-name="success"/> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <mvc:default-servlet-handler/> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <ref bean="employeeConverter"/> </set> </property> </bean> </beans>

SpringMVC自定义类型转化器SpringMVC自定义类型转化器
猜你喜欢