您的位置:首页 > 编程语言 > Java开发

AJAX+SpringMVC 获取后台数据的方式

2017-03-09 16:01 281 查看
利用ajax同步获取服务器的数据,当页面加载完成的时候

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>中介门店注册</title>
<link rel="stylesheet" type="text/css" href="static/css/messageProtect.css" />
<link rel="stylesheet" type="text/css" href="static/css/reset.css" />

<link rel="stylesheet" type="text/css" href="static/lib/laydate/need/laydate.css" />
<script type="text/javascript" src="static/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="static/lib/laydate/laydate.js"></script>
<script src="static/js/city2.js"></script>
<script type="text/javascript" src="static/js/citySelect2.js"></script>
<script type="text/javascript" src="static/js/messageProtect.js"></script>
<script type="text/javascript" src="static/js/plugIn.js"></script>
<script type="text/javascript">

/*当页面加载完成时调用*/
$(document).ready(function(){
getAllSystemData();
});
/*ajax同步发请求(当页面加载的时候)*/
function getAllSystemData(){
$.ajax({
type:"post", /*为post方式*/
async:false, /*开启同步请求,true为异步请求*/
url:"all_system_count",  /*url为发请求的url,利用Controller@RequestMapping进行拦截*/
success:function(data){  /*当请求成功之后回调*/
data = eval("("+data+")");
fillSystemInfo(data.root); /*获取json串,并传给这个方法*/
}
});
}

function fillSystemInfo(data){
var s = "<tr><td>项目总数</td><td>置业顾问总数</td><td>房源总数</td><td>门店总数</td><td>经纪人总数</td><td>合伙人总数</td></tr>";
$.each(data,function(v,o){  /*o为json的数据(后台传过来的)*/
s+='<tr><td>'+o.projectCount+'</td>';
s+='<td>'+o.adviserCount+'</td>';
s+='<td>'+o.houseCount+'</td>';
s+='<td>'+o.shopCount+'</td>';
s+='<td>'+o.agentCount+'</td>';
s+='<td>'+o.partnerCount+'</td></tr>';
});

if(data.length>0){
$("#systemCountInfo").html(s);  /*当服务器有数据传送过来,将所有的元素都添加到id为systemCountInfo中*/
}else{
$("#systemCountInfo").html("<br/><span style='width:10%;height:30px;display:block;margin:0 auto;'>暂无数据</span>");
}
}

</script>
</head>
<body>
<div class="main-content">
<div class="house-list">
<table  id="systemCountInfo" style="table-layout:fixed;">

</table>
</div>
</div>
</body>
</html>


后台代码:

/**
* 平台管理控制器
* @author cdh
*
*/
@Controller("systemController")
public class SystemController extends BaseController{

@Resource(name="systemService")
private SystemService systemService;
/**
* 获取平台管理中项目总数,职业顾问总数...
*/
@RequestMapping("all_system")
public String toSystemPage(){
return "/all_system_count";
}

/*拦截的请求并进行处理*/
@RequestMapping("all_system_count")
public void allSystemCount(){
List<Map<String,String>> list = systemService.getSystemCount();
if(list != null){
this.outListString(list);   /*this是继承的BaseController中的将List转换成json串的方法*/
}

}
}


Service层:

@Service("systemService")
public class SystemServiceImpl implements SystemService{

@Resource(name="baseDao")
private BaseDao baseDao;

@Override
public List<Map<String,String>> getSystemCount() {

List<Map<String,String>> countList = new ArrayList<>();
Map<String,String> countMap = new HashMap<>();
/*获取项目总数*/
List<Project> projectList = baseDao.findByHql("from Project");
Integer projectCount = projectList.size();
System.out.println(projectCount);
/*获取置业顾问总数   role_id=3代表职业顾问*/
List<Role> roleList = baseDao.findByHql("from Role where id = 3");
Role role = roleList.get(0);
Set<User> users = role.getUser();
System.out.println(users.size());
//      List<User> adviser = baseDao.findByHql("from User where roleId = "+uSet);
Integer adviserCount = users.size();
//      System.out.println(adviserCount);
/*获取房源总数*/
List<House> houseList = baseDao.findByHql("from House");
Integer houseCount = houseList.size();
System.out.println(houseCount);
/*门店总数*/
List<Shops> shopList = baseDao.findByHql("from Shops where shopStatus = 1");
Integer shopCount = shopList.size();
System.out.println(shopCount);
/*经纪人总数 roleId=1为中介经纪人*/
List<Role> agentRole = baseDao.findByHql("from Role where id= 1");
Role r = agentRole.get(0);
Set<User> agents = r.getUser();
Integer agentCount = agents.size();
System.out.println(agentCount);
/*合伙人总数  6为合伙人*/
List<Role> partnerRole = baseDao.findByHql("from Role where id = 6");
Role p = partnerRole.get(0);
Set<User> partners = p.getUser();
Integer partnerCount = partners.size();
/*将所有的元素放入Map*/
countMap.put("projectCount", projectCount.toString());
countMap.put("adviserCount", adviserCount.toString());
countMap.put("houseCount", houseCount.toString());
countMap.put("shopCount", shopCount.toString());
countMap.put("agentCount", agentCount.toString());
countMap.put("partnerCount", partnerCount.toString());

/*将Map放入LIst*/
countList.add(countMap);

return countList;

}

}


List转json方法:

public void outListString(List list) {
String jsonString = null;
try {
JSONArray jsonArray = new JSONArray();
if (list.size() > 0) {
jsonArray = JSONArray.fromObject(list);
}
jsonString = "{total:" + list.size() + ",root:"
+ jsonArray.toString() + "}";

} catch (Exception e) {
e.printStackTrace();
}
System.out.println("=============================="+jsonString);
outString(jsonString);
}


学习中。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ajax spring mvc