Practice SPRINGMVC Converter is a problem: form submission does not go through a custom converter (fix: Table is submitted by post only)
Custom Converter Code
Packagecn.liangqinghai.test;ImportOrg.springframework.core.convert.converter.Converter;Importcn.liangqinghai.pojo.Student; Public classMyconverterImplementsConverter<string, student>{@Override PublicStudent Convert (String source) {System.out.println ("What the Custom converter accepts"); 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=NewStudent (Integer.parseint (SID), sname, password); System.out.println ("Converted content:" +student); returnstudent; } return NULL; }}
Controller code
/** *************************** Test Automatic type conversion ************************************************* */ @RequestMapping ("/converter") public String testconverter (@ModelAttribute (" Student ") Student Student) { studentdao.add (Student); return "Redirect:/curd/listall"; }
Spring.xml configuration file
!----><Mvc:annotation-drivenConversion-service= "Conversionservice"></Mvc:annotation-driven> <!--Configuring Custom Converters - <BeanID= "Conversionservice"class= "Org.springframework.context.support.ConversionServiceFactoryBean"> < Propertyname= "Converters"> <List> <Beanclass= "Cn.liangqinghai.test.MyConverter"></Bean> </List> </ Property> </Bean>
Form:
<H1>Test your custom Converter</H1> <formAction= "${pagecontext.request.contextpath}/curd/converter"Method= "POST"> <inputtype= "text"name= "Student"> <inputtype= "Submit"> </form>
Form submission must be post
SPRINGMVC Custom Converters