springmvc 如何展示sql语句查询结果

键上艺术 2017-06-07 11:02:21
在控制类中使用自定义sql语句查询数据库 String sql = "select DATE_FORMAT (t.date,'%Y-%m') month,sum(t.out_total) out_total,sum(enter_total) enter_total from monitor_people_stream t where DATE_FORMAT(date_sub(curdate(),interval 12 month),'%Y-%m') group by month";
但是return modelAndView()不知道怎么把查询结果传递给jsp并展示,jsp用的是easyUI t:datagrid来展示,不管怎么传递,jsp都是展示表中的数据而不是查询结果数据,这要怎么破?
...全文
691 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 2017-06-07
  • 打赏
  • 举报
回复
如果你是easyui那么应该是访问返回一个 json. controller层,应该返回@ResponseBody 例子
@RequestMapping(value = "list.do", method = RequestMethod.POST)
    public @ResponseBody  List<HashMap> getCompanyList(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "AjaxQuery") String AjaxQuery) {

        List<HashMap> list = companySercive.findListByAjaxKey(AjaxQuery);
        return list;// JsonModel.listToJSON(list);

    }
coder_will 2017-06-07
  • 打赏
  • 举报
回复
把数据丢到modelAndView里,页面直接拿EL表达式就可以拿吧。。
刘温柔_ 2017-06-07
  • 打赏
  • 举报
回复
用过Mybatis框架连接数据库查询数据、然后查询到的数据用list接收、再返回给前端、easyUI的datagrid控件只要字段对上就能显示、具体的可以查看API、很详细、我做过类似的项目、需要的话可以给你看代码、
洛鸣生 2017-06-07
  • 打赏
  • 举报
回复
基本的思路就是基于json数据传输,注意的包括序列化反序列化、传输编码、前台展示等等,都是一些细节的问题
licip 2017-06-07
  • 打赏
  • 举报
回复
jsp用的是easyUI t:datagrid来展示 这你要用ajax来实现。 你返回的数据要用json的形式。
NANU-NANA 2017-06-07
  • 打赏
  • 举报
回复
你要把结果存到list<Bean>里,然后把list送到前台
键上艺术 2017-06-07
  • 打赏
  • 举报
回复
控制类为:
	@RequestMapping(params = "queryPeopleStream")
	public  @ResponseBody  List<QueryPeopleYear> queryPeopleStream(QueryPeopleYear QueryPeopleYear,HttpServletRequest request) {
	    String sql = "select DATE_FORMAT (t.date,'%Y-%m') month,sum(t.out_total) out_total,sum(enter_total) enter_total from monitor_people_stream t where DATE_FORMAT(date_sub(curdate(),interval 12 month),'%Y-%m') group by month";
	    String yearSql = "select DATE_FORMAT (t.date,'%Y') year,sum(t.out_total) out_total,sum(enter_total) enter_total  from monitor_people_stream t where DATE_FORMAT(date_sub(curdate(),interval 12 month),'%Y-%m') group by year";
	    List<QueryPeopleYear> queryPeopleStreamList = jdbcDao.find(yearSql, QueryPeopleYear.class, null);
//	    System.out.println("list.size="+queryPeopleStreamList.size());
//	    for(QueryPeopleStreamEntity entity:queryPeopleStreamList){
//	    	System.out.println("统计数据为:"+entity.getEnterTotal()+";"+entity.getDay());
//	    }
	    request.setAttribute("queryPeopleStreamList", queryPeopleStreamList);
	    Map<String,List> model = new HashMap<String,List>();
	    model.put("queryPeopleStreamList", queryPeopleStreamList);
	    String data = JsonUtils.object2Json(queryPeopleStreamList);
	    System.out.println("data = "+data);
	    model.put("queryPeopleList", queryPeopleStreamList);
	    ModelAndView mav = new ModelAndView("monitor/queryPeopleStreamList",model);
	    mav.addObject("queryPeopleList", data);
		return queryPeopleStreamList;
	}
jsp为:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<script src="plug-in/monitor/js/ajax-pushlet-client.js"></script>

<style> 

#main{width:auto;height:auto;} 

#left{width:auto;height:600px;} 

#right{width:auto;height:600px;margin-left:0px;} 

#left,#right{float:left;} 
</style>

<t:base type="jquery,easyui,tools,DatePicker"></t:base>


<div id="main">

<div class="easyui-layout" fit="true"  id="right">
  <div region="center" style="padding:1px;">
  <t:datagrid name="queryPeopleStreamList" title="客流信息查询" actionUrl="queryPeopleStreamController.do?datagrid" idField="id" fit="true">
   <t:dgCol title="编号" field="id" hidden="false"></t:dgCol>
   <t:dgCol title="日期" field="date" formatter="yyyy-MM-dd hh:mm:ss"></t:dgCol>
   <t:dgCol title="总入园人数" field="enterTotal" ></t:dgCol>
   <t:dgCol title="总出园人数" field="outTotal" ></t:dgCol>
   <t:dgCol title="滞园人数" field="leftTotal" ></t:dgCol>
   <t:dgCol title="操作" field="opt" width="100"></t:dgCol> 
   <t:dgDelOpt title="删除" url="queryPeopleStreamController.do?del&id={id}" />
   <t:dgToolBar title="录入" icon="icon-add" url="queryPeopleStreamController.do?addorupdate" funname="add"></t:dgToolBar>
   <t:dgToolBar title="编辑" icon="icon-edit" url="queryPeopleStreamController.do?addorupdate" funname="update"></t:dgToolBar>
   <t:dgToolBar title="查看" icon="icon-search" url="queryPeopleStreamController.do?addorupdate" funname="detail"></t:dgToolBar>
  </t:datagrid>
  </div>
 </div>
 </div>
 

 <script type="text/javascript">
//  function getRtmMsg(show){
// 	    $.ajax({
// 		   type:"POST",
// 		   url:"monitorPeopleStreamController.do?monitorPeopleStream",
// 		   data:"show="+show,
// 		   success: function(msg){
// 		     if(msg!=null){
// 		     	bottomRight();
// 		     	getRtmMsg("0");
// 		     }
// 		   }
// 		});
//     }
    
//     function bottomRight(){  
// 	    alert("aaaaa");
//     }
// $(function(){
//     PL._init();   
//     PL.joinListen('/getNewMessage');  
//     PL.setFun(onData);
// });

// function onData(event) {   
// 	var tipMessage = event.get("message");
// 	if(tipMessage != undefined && tipMessage != ""){
// 	alert("aaa");
// 		showMessage("收到新消息:【" + tipMessage + "】");
// 	}
// }  
	PL._init(); 
			PL.joinListen('/cuige/he');
			function onData(event) { 
				alert(event.get("mess")); 
				// 离开
				// PL.leave();
			}
 </script>
这是jeecg的开源框架,我想重用jsp的datagrid ,但是不管控制类的业务逻辑怎么写,都是显示数据库的表数据

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧