Ajax passes the value to the spring background (JSON), the parameters are automatically converted to objects, and the @requestbody gets the __JS in the controller

Source: Internet
Author: User

The project used AJAX for page data list refresh, call need to use a lot of query conditions, the previous practice is to use @requestparam to get the incoming parameters, but more parameters, the number of @RequestParam will be more and more, You can use @requestbody to receive the list of parameters in Jason format and automatically convert to a custom object, but you need to introduce Jackson


<dependency>
	        <groupId>com.jayway.jsonpath</groupId>
			<artifactid>json-path</ artifactid>
			<version>0.8.1</version>
			<scope>test</scope>
		</ dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			< artifactid>jackson-core-lgpl</artifactid>
			<version>1.8.5</version>
		</ dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			< artifactid>jackson-mapper-lgpl</artifactid>
			<version>1.8.5</version>
		</ Dependency>

This is written in Ajax: The field in the red font is the same as the fields in the Java bean, so that Jackson can automatically convert

function GetPageSize () {
		$.ajax ({  
	        type: Post),  
	        async:false,  
	        url: ' getpagesize ',  
	        data: Json.stringify ({
	        	SourceID: $ ("#source"). Val (),
	        	Seriesid: $ ("#series"). Val (),
	        	modelid: $ ("#model"). val (), City
	        	: $ ("#city"). Val (),
	        	strfrom: $ ("#from"). Val (),
	        	Strto: $ ("#to"). Val ()
	        }),
	        DataType: "JSON", 
	        contentType: "Application/json;" Charset=utf-8 ", 
	        success:function (data) {
	        	...}
	        ,
	        error:function (data) {
	        	...
	        }}
	    );
	}

This is written in spring controller:

@RequestMapping (value = "/queryinfo", method = Requestmethod.post)
	@ResponseBody public
	list<record> QueryInfo (@RequestBody searchcondition condition) {
               //...
}

This allows the Ajax data to be converted to Java objects, without a single parameter to get.


Related Article

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.