SpringMVC Ajax request data and the process returns json

Here I spent three forms returned 1, String 2, 3 returns the object and returns an array

First, the way to return String

Look at the JavaScript code

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
	$(function(){
		$("input[name=uname]").blur(function(){
			var name = $(this).val();
			$.ajax({
				url:"ajaxC/checkName",
				type:"post",
				data:"uname="+name,
				datatype:"html",
				success:function(res){
					$("#show").html(res)
				}
			})
		})
		
		$("button[name=bb]").click(function(){
			$.ajax({
				url:"ajaxC/checkName1",
				type:"post",
				datatype:"json",
				success:function(res){
					alert(res.id+"----"+res.name+"----"+res.age)
				}
			})
		})
		
		$("button[name=btn]").click(function(){
			var name = $(this).val();
			$.ajax({
				url:"ajaxC/checkName2",
				type:"post",
				datatype:"json",
				success:function(res){
					$(res).each(function(i,u){
						alert(u.id+"----"+u.name+"----"+u.age)
					})
				}
			})
		})
		
	})
</script>
<body>
	用户名:<input name="uname"/><span  id="show"></span><button name="bb" type="button" >获取单个信息</button><button name="btn" type="button" >获取集合信息</button>
</body>
</html>

Here I use Ajax asynchronous requests to the back of the console controller, first Ajax is to determine the legality of the user name, and the second is to receive backstage pass over Ajax object data, and the third is receiving a pass over the background of the set.

Please see below controller console code

Annotation @ResponseBody the method body, return type may also be Map, Array like. The processor is configured to help us json JSON format string, reduce the workload.
package com.direct.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.direct.entity.UserInfo;

@Controller
@RequestMapping(value="/ajaxC")
public class AjaxController {
	@RequestMapping(value="/checkName",method=RequestMethod.POST)
	@ResponseBody
	private String  checkName(String uname) {
		if(uname!=null && uname.matches("[a-zA-Z0-9]{6,}")){
			return "<span style='color: green;'>用户名可用</span>";
		}
		return "<span style='color: red;'>用户名不可用</span>";
	}
	
	@RequestMapping(value="/checkName1",method=RequestMethod.POST)
	@ResponseBody
	private UserInfo  checkName1() {
		
		UserInfo user = new UserInfo(1, "战三", 18, "男");
		
		return user;
	}
	
	
	@RequestMapping(value="/checkName2",method=RequestMethod.POST)
	@ResponseBody
	private List<UserInfo>  checkName2() {
		List<UserInfo> ulist = new ArrayList<>();
		ulist.add(new UserInfo(1, "战三", 18, "男"));
		ulist.add(new UserInfo(2, "战三2", 16, "男"));
		ulist.add(new UserInfo(3, "战三3", 12, "女"));
		ulist.add(new UserInfo(4, "战三4", 20, "男"));
		return ulist;
	}
}

Ajax ordinary asynchronous request is different, the HttpServletResponse SPRINGMVC need not be employed, only need to add annotations @ResponseBody in the above method can be a need to configure the processing returns ResponseBody json encoding format and the conversion SpringMCV After the addition, the following process is ResponseBody json return code conversion and encoding format:

<!-- 处理ResponseBody返回编码格式 -->
		<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
			<property name="messageConverters">
				<list>
					<bean class="org.springframework.http.converter.StringHttpMessageConverter">
						<property name="supportedMediaTypes">
							<list>
								<value>text/html;charset=UTF-8</value>
							</list>
						</property>
					</bean>
					<!-- SpringMVC的json转换 -->
					<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean>
				</list>
			</property>
		</bean>

PS: If you have something to deal with static resources in your SpringMVC configuration <mvc:annotation-driven/>requires you deal ResponseBody return code encoding format on top of it, it still will be Chinese garbled.

Guess you like

Origin blog.csdn.net/Rm_and_Rf/article/details/90402912