SSM中,返回值为Void,向前端传递数据的方法

Controller层中的代码:

    @RequestMapping("/selectUser")
	public void selectAll(){
        response.setContentType("text/text"); //设置请求以及响应的内容类型以及编码方式
        response.setCharacterEncoding("UTF-8");
        JSONArray json = JSONArray.fromObject(newsList); //将查询出的newsList对象转换为json对象
        String str = json.toString(); //将json对象转换为字符串
        response.getWriter.write(str); //将str字符传输到前台
	}

前端Ajax中的代码:

$.ajax({

type: 'post',
url: '/selectUser',
dataType: 'json',
success:function(data){
  var item;
$.each(data,function(i,result){ //将后台传递过来的数据进行遍历
item="<tr><td>"+result['name']+"</td><td>"+result['age']+"</td></tr>"; 
$('.table').append(item); 
}); 
},

});

jsp中的代码:

<table class="table"> 
<tr><th>name</th><th>age</th></tr> 
</table> 

转载来自:http://blog.csdn.net/qq_26079137

猜你喜欢

转载自blog.csdn.net/MyBloggerlxs/article/details/81623884