SPRINGMVC receive Array type parameters

Source: Internet
Author: User
Tags webpagetest

You cannot instantiate an error by using the List<bean> report directly on the method!

List<bean> must be placed in an object to be accepted as a property. See the example below for details

Front-end Code

<!DOCTYPE HTML><HTML><Head>    <MetaCharSet= "UTF-8">    <title>Page</title></Head><Body><H1>Array Pass parameter test</H1><Scriptsrc= "/webpagetest/js/jquery-2.0.3.min.js"></Script><Scripttype= "Text/javascript">    functionTest () {$.ajax ({URL:'/webpagetest/pagedata', data:{'ID':1,                'Order[0].column':'name',                'Order[0].dir':'ASC',                'Order[1].column':'Phone',                'Order[1].dir':'desc'}, Success:function(RT) {alert (RT); }, Error:function() {alert ('Error');    }        }); }    functiontest2 () {$.ajax ({URL:'/webpagetest/pagedata2', data:{'ID':1,                'Order[0][column]':'name',                'Order[0][dir]':'ASC',                'Order[1][column]':'Phone',                'Order[1][dir]':'desc'}, Success:function(RT) {alert (RT); }, Error:function() {alert ('Error');    }        }); }</Script><Buttononclick= "Test ()">Test</Button><Buttononclick= "test2 ()">Test 2</Button></Body></HTML>

Background code

@RequestMapping ("/pagedata") @ResponseBody PublicString Index (@RequestParam (value= "Order[0].column", required =false) String Order0column, httpservletrequest request, Myvo vo) {Syst Em.out.println ("Get a single parameter:");        System.out.println (Order0column); System.out.println ("Get All Parameters:"); Map<String,String[]> params =Request.getparametermap ();  for(map.entry<string,string[]>Entry:params.entrySet ()) {System.out.println ("Key:" +entry.getkey () + "value:" +arrays.aslist (Entry.getvalue ())); } System.out.println ("Get Vo"); System.out.println ("ID:" +Vo.getid ()); System.out.println ("Orders:");  for(Order o:vo.getorder ()) {System.out.println ("Column:" + o.getcolumn () + "dir:" +O.getdir ()); }        return"OK"; } @RequestMapping ("/pagedata2") @ResponseBody PublicString Index (@RequestParam (value= "order[0][column]", required =false) String Order0column, httpservletrequest request/*, MyVO2 VO2*/) {System.out.println ("Get a single parameter:");        System.out.println (Order0column); System.out.println ("Get All Parameters:"); Map<String,String[]> params =Request.getparametermap ();  for(map.entry<string,string[]>Entry:params.entrySet ()) {System.out.println ("Key:" +entry.getkey () + "value:" +arrays.aslist (Entry.getvalue ())); }//System.out.println ("Get VO2");//System.out.println ("ID:" +vo2.getid ());//System.out.println ("Orders:");//For (list<string> O:vo2.getorder ()) {//System.out.println ("column:" + o.get (0) + "dir:" +o.get (1));//        }        return"OK"; }

Two entity object (must be a class in a separate file, otherwise the constructor's error cannot be reported)

 Public classmyvo{Private intID; PrivateList<order> Order =NewArraylist<order>();  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicList<order>GetOrder () {returnorder; }     Public voidSetorder (list<order>order) {         This. Order =order; }}
 Public classorder{PrivateString column; PrivateString dir;  PublicString GetColumn () {returncolumn; }     Public voidsetcolumn (String column) { This. Column =column; }     PublicString Getdir () {returndir; }     Public voidSetdir (String dir) { This. Dir =dir; }}

SPRINGMVC receive Array type parameters

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.