得有相关的json JAR包。json-lib和jackson2是不一样的。分清楚。
代码可以完美运行滴,可复制粘贴!前提你的相关配置没有错哦,数据库和后台,前台和后台都要连通。
先看看controller的代码,
我用@RequestMapping+@RequestBody将前台收来的表单数据封装成一个pojo,然后用Mybtis框架去数据库查找对应记录,最后包在ModelAndView里面返回给前端。
package com.ssm.controller;
import com.mysql.cj.xdevapi.JsonArray;
import com.ssm.pojo.userPojo;
import com.ssm.service.userService;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONString;
import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
//import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import javax.ann