Spring MVC Ajax request

Code example

  Web-side code:

<body>
	<input name="user" type="text"/>
	<button onclick="test1()">ajax测试</button>
	<div id="ttt">Java 端返回的数据将在这里显示	</div>
	<script type="text/javascript">
		function test1(){
     
     
			$.ajax({
     
     
				type:"post",
				data:"user="+$("input[name='user']").val(),
				url:"${pageContext.request.contextPath}/ajax/ajax1.do",
				dataType:"text",
				success:function (data){
     
     
					console.log(data);
					$("#ttt").html(data);
				}
			});
		}
	</script>
</body>

  Java side code:

@Controller
@RequestMapping("/ajax")
public class AjaxController {
    
    
	@RequestMapping(value = "/ajax1", method = RequestMethod.POST)
	@ResponseBody
	public String ajax1(@RequestParam(value = "user") String name) {
    
    
		System.out.println(name);
		return "aaaa";
	}
}

  In the @RequestMapping annotation method = RequestMethod.POST, it means that the request mode is post mode.

  The function of the @ResponseBody annotation is to convert the object returned by the controller method into a specified format through an appropriate converter, and then write it to the body area of ​​the response object. It is usually used to return JSON data.
  Note: After using this annotation, you will not go to the view processor, but directly write the data to the input stream. Its effect is equivalent to outputting the data in the specified format through the response object, which is the form shown in the code below.

@RequestMapping("/ajax2")
public void ajax2(String code, HttpServletResponse response) {
    
    
	System.out.println(code);
	try {
    
    
		response.getWriter().append("bbbb");
	} catch (IOException e) {
    
    
		e.printStackTrace();
	}
}

  If the Ajax request returns an object in JSON format and contains Chinese , then you need to add the produces parameter in @RequestMapping("/ajax2"), otherwise the Chinese cannot be transmitted correctly.

@RequestMapping(value = "/ajax1", method = RequestMethod.POST, 
					produces = "application/json;charset=utf-8")

  If the Ajax request returns text and contains Chinese

@RequestMapping(value = "/ajax1", method = RequestMethod.POST, 
					produces = "text/html;charset=utf-8")

Automatically convert objects into JSON format data

  Spring MVC can use the JSON jar package mentioned above to convert Java objects or collections into JSON format data, and then convert them into String type data for output.
  But Spring MVC has a simpler way to implement it.

Ready to work

  In the pom.xml file, introduce jackson-databind dependency.

<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>2.9.6</version>
</dependency>

use

@RequestMapping(value = "/ajax1", method = RequestMethod.POST,
					produces = "application/json;charset=utf-8")
@ResponseBody
public PersonModel ajax2(@RequestParam(value = "user") String name) {
    
    
	PersonModel pm = new PersonModel();
	pm.setName(name);
	return pm;
}

  Spring MVC will automatically convert Java objects into JSON format based on the dependencies we just introduced.

Guess you like

Origin blog.csdn.net/qq_40395874/article/details/114467121