The delivery of synchronous/asynchronous request parameters in Springmvc and the return of data

Source: Internet
Author: User
Tags string to json

Note: The return to the JSP page is returned here

How the controller receives foreground data and passes the processed model to the front desk * * * *

1. Reception of data received by the foreground: the property name of the pass is the same as the JavaBean attribute

(1), use basic type, or reference type to accept:

@RequestMapping (value= "/select")

Publicstring Select (String name,int age,model Model) {

So the name,age here is our foreground pass the parameter, also is our JavaBean the attribute.

So you can pass the value to the foreground.

Model.addattribute ("user", user);

Return "List";

}

}

(2), the use of Pojo way to accept: here, the value of the foreground property is the same as the JavaBean property name

This method is used more

@RequestMapping (value= "select")

Publicstring Select (user user, model model) {

Stringname= User.getname ();

Stringage=user.getage ();

Follow these properties to query, then put the data collection in model

list<map<string,object>> list= new arraylist<map<string,object>> ();

list= "Search results by condition";

Model.addattribute ("userlist", list);

Return "List";

}


(3), if the foreground has the same name value of the data passing is to use the array to receive, the same name may have such a value but more is a multi box, so we want to use an array to receive such a value

@RequestMapping (value= "select.do")

Public Stringhello (String []username,model Model) {

System.out.println (username.length);

STRINGNAME1=USERNAME[0];

STRINGNAME2=USERNAME[1];

Model.addattribute ("HelloWorld", "Hello" +name1+name2);

return "Welcome";

}

(4), this way and the servlet accepts the request parameters in the same way, because SPRINGMVC provides the servlet API so we can use HttpServletRequest request to get the parameters

@RequestMapping (value= "/hello.do")

publicstring Hello (httpservletrequest request,model Model) {

String name=request.getparameter ("name");

SYSTEM.OUT.PRINTLN (name);

Model.addattribute ("HelloWorld", "Hello" +name);

return "Welcome";

}

We've all seen it. The parameter acceptance method is listed above, but we find that the acceptance of the model (data model) is transmitted using the model object, which is also used for push, but there are other ways, in fact, regardless of the way the technical implementation is the same, the parameter delivery specific way: * *****

2, the data model to the foreground value:

(1), passing a bean or basic type value to the foreground

@RequestMapping (value= "select")

Use model to pass objects to foreground

Publicstring Select (user user, model model) {

Stringname= User.getid ();

Follow these properties to query, then put the data in model

User user= "Data obtained by ID"

Model.addattribute ("user", user);

Return "List";

}

(2), to the front desk list collection

@RequestMapping (value= "/hello.do")

Public String Hello (User user,modelmodel) {

List<map<string,object>> list=new arraylist<map<string,object>> ();

Map<string,object> map1= newhashmap<string,object> ();

Map<string,object> map2= newhashmap<string,object> ();

Map1.put ("name", "name1");

Map1.put ("Age", 1);

Map2.put ("name", "name2");

Map2.put ("Age", 2);

List.add (MAP1);

List.add (MAP2);

The list collection is placed in the model, and the collection is fetched in the foreground and traversed

Model.addattribute ("ulist", list);

return "Welcome";

}

The front desk uses the C tag

<c:foreach items= "${ulist}" var= "User" >

${user.name}

${user.age}

</c:forEach>

Several of the previous models use the model to pass the value, which is also a way to compare the push, but there are several ways to be listed here:

(3), modelandview from the literal meaning we can see this object includes, the data model and the view name

Public Modelandview Select (user user) {

Modelandview mav=new Modelandview ("View_value");

The view can be written as above, or it can be written as follows, that is, two ways to assign a view

Mav.setviewname ("View_value"); Specify the view name

Mav.addobject ("Key1", value);

Mav.addobject ("Key2", value);

return modelandview;

}

(4), use map to store model and pass value to foreground

@RequestMapping (value= "/hello.do")

Public String Hello (User user,map<string,object> Map) {

List<map<string,object>> list=new arraylist<map<string,object>> ();

Map<string,object> map1= newhashmap<string,object> ();

Map<string,object> map2= newhashmap<string,object> ();

Map1.put ("name", "name1");

Map1.put ("Age", 1);

Map2.put ("name", "name2");

Map2.put ("Age", 2);

List.add (MAP1);

List.add (MAP2);

The list collection is placed in the model and the collection is traversed at the foreground, and the map and model are very similar.

Map.addattribute ("ulist", list);

return "Welcome";

}

These are all synchronous requests, so let's look at the delivery and reception of Ajax asynchronous request data *************

    • Ajax request to the background value, the foreground to pass the general value
    • Ajax request to background, foreground pass JSON format value

1, Ajax request to the background value of the general format, the general format is the Jsonobject format of the data

$.ajax ({

URL: "Selectinfo.do",

Type: "Post",

Data is a value passed to the background, with name and age two values

Data:{name: "Name1", Age:2},

Specifies the type of return value, which is returned in JSON format

DataType: "JSON",

Data is the value passed back to the background

Success:function (data) {

It's a jsonarray in the background. Use the Eval function to traverse this

Arr=eval (data);

Alert ("Length" +arr.length);

Alert (arr);

for (Var i=0;i<arr.length;i++) {

Alert ("Content" +arr[i].title);

}

}

});

Background acceptance, where the acceptance of the data and the previous data received is the same, that can use request, variables, Javabean, etc. to receive

(1), using request to receive

@RequestMapping (value= "selectinfo.do", Method=requestmethod.post)

public void Selectinfo (HttpServletRequest request,httpservletresponse response) {

Stringname= request.getparameter ("name");

Stringage= request.getparameter ("Age");

System.out.println ("V" +name);

System.out.println ("V" +age);

list<map<string,object>>list= new arraylist<map<string,object>> ();

map<string,object>map1= new hashmap<string,object> ();

map<string,object>map2= new hashmap<string,object> ();

map<string,object>map3= new hashmap<string,object> ();

Map1.put ("title", "Map1");

Map1.put ("Artist", "ADG3");

Map1.put ("MP3", "Musics/zjh.mp3");

Map1.put ("poster", "images/m0.jpg");

Map2.put ("title", "Map2");

Map2.put ("Artist", "ADG3");

Map2.put ("MP3", "Musics/zhm.mp3");

Map2.put ("poster", "images/m0.jpg");

Map3.put ("title", "Warm Heart");

Map3.put ("Artist", "ADG3 Studios");

Map3.put ("MP3", "Musics/zhm.mp3");

Map3.put ("poster", "images/m0.jpg");

List.add (MAP1);

List.add (MAP2);

List.add (MAP3);

Convert to JSON array

Jsonarraystr=jsonarray.fromobject (list);

try {

Output value to foreground, get PrintWriter object

Printwriterout= Response.getwriter ();

Data in the success function of the AJAX is output to this value

Out.print (Str.tostring ());

}catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

System.out.println (Str.tostring ());

}

(2), use variable string name int age to receive

@RequestMapping (value= "selectinfo.do", Method=requestmethod.post)

public void Selectinfo (String name,int age,httpservletresponse response) {

Stringname= request.getparameter ("name");

Stringage= request.getparameter ("Age");

System.out.println ("V" +name);

System.out.println ("V" +age);

list<map<string,object>>list= new arraylist<map<string,object>> ();

map<string,object>map1= new hashmap<string,object> ();

map<string,object>map2= new hashmap<string,object> ();

map<string,object>map3= new hashmap<string,object> ();

Map1.put ("title", "Map1");

Map1.put ("Artist", "ADG3");

Map1.put ("MP3", "Musics/zjh.mp3");

Map1.put ("poster", "images/m0.jpg");

Map2.put ("title", "Map2");

Map2.put ("Artist", "ADG3");

Map2.put ("MP3", "Musics/zhm.mp3");

Map2.put ("poster", "images/m0.jpg");

Map3.put ("title", "Warm Heart");

Map3.put ("Artist", "ADG3 Studios");

Map3.put ("MP3", "Musics/zhm.mp3");

Map3.put ("poster", "images/m0.jpg");

List.add (MAP1);

List.add (MAP2);

List.add (MAP3);

Convert to JSON array

Jsonarraystr=jsonarray.fromobject (list);

try{

Get PrintWriter Object

Printwriterout= Response.getwriter ();

Output data

Out.print (Str.tostring ());

}catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

System.out.println (Str.tostring ());

}

(3), using JavaBean to receive

@RequestMapping (value= "selectinfo.do", Method=requestmethod.post)

public void Selectinfo (User user,httpservletresponse response) {

System.out.println ("V" +user.getname ());

System.out.println ("V" +user.getid ());

list<map<string,object>>list= new arraylist<map<string,object>> ();

map<string,object>map1= new hashmap<string,object> ();

map<string,object>map2= new hashmap<string,object> ();

map<string,object>map3= new hashmap<string,object> ();

Map1.put ("title", "Map1");

Map1.put ("Artist", "ADG3");

Map1.put ("MP3", "Musics/zjh.mp3");

Map1.put ("poster", "images/m0.jpg");

Map2.put ("title", "Map2");

Map2.put ("Artist", "ADG3");

Map2.put ("MP3", "Musics/zhm.mp3");

Map2.put ("poster", "images/m0.jpg");

Map3.put ("title", "Warm Heart");

Map3.put ("Artist", "ADG3 Studios");

Map3.put ("MP3", "Musics/zhm.mp3");

Map3.put ("poster", "images/m0.jpg");

List.add (MAP1);

List.add (MAP2);

List.add (MAP3);

Convert to JSON array

Jsonarraystr=jsonarray.fromobject (list);

try{

Get PrintWriter Object

Printwriterout= Response.getwriter ();

Output data

Out.print (Str.tostring ());

}catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

System.out.println (Str.tostring ());

}

2. Ajax requests the value of the JSON array format to be passed back to the background:

(1), in the foreground Ajax send asynchronous request, the format of passing data is Jsonarray, the specific code is as follows:

<script type= "Text/javascript" >

var arr=null;

$ (document). Ready (function () {

Data in a jsonarray format is stored in the Jsonobject

varallmenu1={"Datas": [{title: "Title1", Age:1},{title: "Title2", Age:2},{title: "Title3", Age:3}]};

/* Convert a string to a JavaScript object */

var arrs = json.stringify (allMenu1);

/* Send asynchronous request to get Jsonarray data */

$.ajax ({

URL: "Selectinfo.do",

Type: "Post",

Data: "Val=" +arrs,

DataType: "JSON",

Success:function (data) {

Arr=eval (data);

Alert ("Length" +arr.length);

Alert (arr);

for (Var i=0;i<arr.length;i++) {

Alert ("Content" +arr[i].title);

}

}

});

});

</script>

(2), background receive the Jsonarray array format data passed by the foreground

@RequestMapping (value= "selectinfo.do", Method=requestmethod.post)

public void Selectinfo (HttpServletRequest request,httpservletresponse response) {

A keyword gets the data in a Jsonobject object format.

Stringstring =request.getparameter ("Val");

Jsonobject json = new Jsonobject ();

Convert a string to JSON

Json=json.fromobject (string);

The key to this Jsonobject object is datas and value is a jsonarray, which is obtained through the critical sub Jsonarray

Jsonarray arr= (Jsonarray) json.get ("Datas");

Gets the jsonobject data in the array

Jsonobject obj1= arr.getjsonobject (0);

Jsonobject obj2= Arr.getjsonobject (1);

Stringname1=obj1.getstring ("title");

Stringname2=obj2.getstring ("title");

SYSTEM.OUT.PRINTLN ("JSON data 1" +name1);

SYSTEM.OUT.PRINTLN ("JSON data 2" +name2);

list<map<string,object>>list= new arraylist<map<string,object>> ();

map<string,object>map1= new hashmap<string,object> ();

map<string,object>map2= new hashmap<string,object> ();

map<string,object>map3= new hashmap<string,object> ();

Map1.put ("title", "Map1");

Map1.put ("Artist", "ADG3");

Map1.put ("MP3", "Musics/zjh.mp3");

Map1.put ("poster", "images/m0.jpg");

Map2.put ("title", "Map2");

Map2.put ("Artist", "ADG3");

Map2.put ("MP3", "Musics/zhm.mp3");

Map2.put ("poster", "images/m0.jpg");

Map3.put ("title", "Warm Heart");

Map3.put ("Artist", "ADG3 Studios");

Map3.put ("MP3", "Musics/zhm.mp3");

Map3.put ("poster", "images/m0.jpg");

List.add (MAP1);

List.add (MAP2);

List.add (MAP3);

Convert list collection to Jsonarray array

Jsonarraystr=jsonarray.fromobject (list);

try {//The biggest difference between Ajax asynchronous requests and synchronous requests is the output of this data, and the output of the AJAX asynchronous request data uses the print output stream object to output the data

Printwriterout= Response.getwriter ();

Out.print (Str.tostring ());

}catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

System.out.println (Str.tostring ());

}

The delivery of synchronous/asynchronous request parameters in Springmvc and the return of data

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.