标签:没有 ack pac ota dir dao strong core style
实践SpringMVC转化器是遇到的问题:表单提交没有经过自定义转换器(解决:表单用post方式提交)
自定义转化器代码
package cn.liangqinghai.test; import org.springframework.core.convert.converter.Converter; import cn.liangqinghai.pojo.Student; public class MyConverter implements Converter<String, Student>{ @Override public Student convert(String source) { System.out.println("自定义转换器接受到的内容"); String[] val = null; if(source != null && !"".equals(source)){ val = source.split("-"); String sid = val[0]; String sname = val[1]; String password = val[2]; Student student = new Student(Integer.parseInt(sid), sname, password); System.out.println("转换后的内容:" + student); return student; } return null; } }
控制器代码
/*****************************测试自动类型转换**************************************************/ @RequestMapping("/converter") public String testConverter(@ModelAttribute("Student")Student student){ studentDao.add(student); return "redirect:/curd/listAll"; }
spring.xml配置文件
!-- --> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <!-- 配置自定义转换器 --> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="cn.liangqinghai.test.MyConverter"></bean> </list> </property> </bean>
表单:
<h1>测试自定义转换器</h1> <form action="${pageContext.request.contextPath }/curd/converter" method="post"> <input type="text" name="Student"> <input type="submit"> </form>
表单提交方式必须为POST
标签:没有 ack pac ota dir dao strong core style
原文地址:http://www.cnblogs.com/liangqinghai/p/6995022.html