SPRINGMVC Custom Converters

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.