SpringMVC页面Ajax传JSON到controller,返回包含多个对象的JSON

做的一个练习,筛选,页面随意勾选多个复选框,局部刷新返回显示与复选框value值匹配的对象数据,传JSON到controller,返回包含多个对象的JSON。controller怎么写?返回JSON后页面怎么处理

$.ajax({
url:"user/screening",

type:"post", //请求方式
data:"{\"pwd1\":\""+pwd1+"\",\"pwd2\":\""+pwd2+"\",\"email1\":\""+email1+"\",\"email2\":\""+email2+"\"}",//要发送的数据
dataType:"", //服务器返回的数据类型
//data返回的数据,strStatus状态码(200响应正确),xhr:XMLHttpRequest对象
success:function(data,strStatus,xhr){ //请求成功返回数据后执行的代码
eval("var mydata="+data); //eval()将字符串当成JS脚本执行
$.each(mydata,function(i,item){ //i:遍历的下标,item:遍历出的单个对象
$("#show").html(item.id+item.username+item.password+item.email);
});
},
error:function(xhr,strStatus,strError){ //请求失败后执行的代码
alert(strError);
}
});

public UserController(){
userMap.put("1", new User("1","yiyiyi","123456","yiyiyi@qq.com"));
userMap.put("2", new User("2","ererer","123456","ererer@qq.com"));
userMap.put("3", new User("3","sansansan","123456","sansansan@qq.com"));
userMap.put("4", new User("4","yiyiyi","654321","yiyiyi@qq.com"));
userMap.put("5", new User("5","ererer","654321","ererer@qq.com"));
userMap.put("6", new User("6","sansansan","654321","sansansan@qq.com"));
}

@RequestMapping(value="screening",method=RequestMethod.POST)
@ResponseBody
public Object screening(@RequestBody Object object){

    JSONObject jo = JSONObject.fromObject(toString());
    List<Object> objects = new ArrayList<Object>();
    Screening scr = (Screening)JSONObject.toBean(jo, Screening.class);
    Iterator<Map.Entry<String, User>> iter = userMap.entrySet().iterator(); 
        while(iter.hasNext()){
            Map.Entry<String, User> entry = iter.next();
            if(scr.getPwd1().equals(entry.getValue().getPassword())
               || scr.getPwd2().equals(entry.getValue().getPassword())
               || scr.getEmail1().equals(entry.getValue().getEmail())
               || scr.getEmail2().equals(entry.getValue().getEmail())){
                objects.add(userMap.get(entry.getKey()));
            }
        }
    return objects;
}

public class Screening {

String pwd1;
String pwd2;
String email1;
String email2;

1个回答

可以用有个list存放再转为json传递

u011112545
u011112545 修改过了,是这么写吗
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问