// 主页 @RequestMapping(value = "/home") public ModelAndView home() { ModelAndView MV = new ModelAndView("loan/loan_list"); return MV; } public LoanSummaryForm setForm(LoanSummary entity) { LoanSummaryForm page=new LoanSummaryForm(); BigDecimal num=new BigDecimal(100); page.setiAutoID(entity.getiAutoID()); page.setsMobile(entity.getsMobile()); page.setiAmount(new BigDecimal(entity.getiAmount()).divide(num)); page.setiMoney(new BigDecimal(entity.getiMoney()).divide(num)); page.setsBankName(entity.getsBankName()); page.setsBankCard(entity.getsBankCard()); page.setiGetCashTime(DateUtil.date2String(new Date(entity.getiGetCashTime()*1000), DateUtil.TIME_FORMATTERN)); page.setiStatus(entity.getiStatus()); page.setiCreateTime(DateUtil.date2String(new Date(entity.getiCreateTime()*1000), DateUtil.TIME_FORMATTERN)); page.setiUpdateTime(DateUtil.date2String(new Date(entity.getiUpdateTime()*1000), DateUtil.TIME_FORMATTERN)); return page; } // 列表 @RequestMapping(value = "/list", method = RequestMethod.POST) @ResponseBody public Map<String, Object> listAll(@RequestBody LoanSummaryForm model,HttpSession session,HttpServletRequest request){ LOGGER.info("新增"); LOGGER.info(" 捕获到前台传递过来的Model,名称为:提现申请时间" + model.getiGetCashTimeFrom() +" 提现申请时间至"+model.getiGetCashTimeTo() +" 提现状态"+model.getiStatus() +" 提现用户账号"+model.getsMobile() ); String sMobile=model.getsMobile(); String iGetCashTimeFrom=model.getiGetCashTimeFrom(); String iGetCashTimeTo=model.getiGetCashTimeTo(); String iStatus=""+model.getiStatus(); List<LoanSummary> list = loansService.findByCondition(sMobile, iGetCashTimeFrom, iGetCashTimeTo, iStatus); List<LoanSummaryForm> pages = new ArrayList<LoanSummaryForm>(); BigDecimal decimal=new BigDecimal(0.00); for (LoanSummary entity : list) { LoanSummaryForm page=setForm(entity); decimal=decimal.add(page.getiMoney()); pages.add(page); LOGGER.error(" 提现流水号:"+page.getiAutoID() +" 提现用户账号:"+page.getsMobile() +" 账户余额(¥):"+page.getiAmount() +" 提现金额(¥):"+page.getiMoney() +" 提现银行:"+page.getsBankName() +" 银行卡号:"+page.getsBankCard() +" 提现申请时间:"+page.getiGetCashTime() +" 提现状态:"+page.getiStatus() +" 创建时间:"+page.getiCreateTime() +" 更新时间:"+page.getiUpdateTime()); } Map<String, Object> modelMap = new HashMap<String, Object>(3); modelMap.put("total", "1"); modelMap.put("data", pages); modelMap.put("success", "true"); modelMap.put("iTotal", decimal.toString()); session.setAttribute("loans", pages); return modelMap; } // ajax请求根据id查找实体数据value = "/findLoan") @RequestMapping(value = "/findLoan", method = RequestMethod.GET) @ResponseBody public Map<String, Object> find(HttpSession session,HttpServletRequest request){ int id= Integer.parseInt(request.getParameter("id")); System.out.println("session set:"+id+"=="); LoanSummary loan = loansService.findById(id); LOGGER.error(" 提现流水号:"+loan.getiAutoID() +" 提现用户账号:"+loan.getsMobile() +" 账户余额(¥):"+loan.getiAmount() +" 提现金额(¥):"+loan.getiMoney() +" 提现银行:"+loan.getsBankName() +" 银行卡号:"+loan.getsBankCard() +" 提现申请时间:"+loan.getiGetCashTime() +" 提现状态:"+loan.getiStatus() +" 创建时间:"+loan.getiCreateTime() +" 更新时间:"+loan.getiUpdateTime()); session.setAttribute("ROOMNAME", id); //springmvc 返回信息到ajax: List<LoanSummary> list = new ArrayList<LoanSummary>(); list.add(loan); Map<String, Object> modelMap = new HashMap<String, Object>(3); modelMap.put("total", "1"); modelMap.put("data", list); modelMap.put("success", "true"); return modelMap; }
<#setting classic_compatible=true> <#assign base=springMacroRequestContext.getContextUrl("")> <#import "spring.ftl" as s /> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>列表</title> <script type="text/javascript" src="<@s.url '/res/jquery-1.11.3.js'/>"></script> <script type="text/javascript" src="<@s.url '/res/jquery.json.min.js'/>"></script> <script type="text/javascript"> $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name]) { if (!o[this.name].push) { o[this.name] = [ o[this.name] ]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }; </script> <style type="text/css"> <!-- .STYLE1 { font-family: Arial, Helvetica, sans-serif,新宋体; font-weight: bold; font-size: 26px; color: #FF0000; } .STYLE13 { font-size: 22 } .STYLE15 { font-family: Arial, Helvetica, sans-serif; font-size: 20px; } --> </style> </head> <body> <form action="list" method="post" id="inform"> <table> <tr> <th>提现用户账号</th> <td><input type="text" name="sMobile" /></td> </tr> <tr> <th>提现申请时间起</th> <td><input type="text" name="iGetCashTimeFrom" /></td> </tr> <tr> <th>提现申请时间止</th> <td><input type="text" name="iGetCashTimeTo" /></td> </tr> <tr> <th>提现状态</th> <td><input type="text" name="iStatus" /></td> </tr> <tr> <td><input type="submit" value="查询"></td> <td><input type="button" onclick="setshow()" value="下载"></td> </tr> </table> </form> <#if loans?? & loans?size gt 0> <p>共查询到items?size}个结果</p> <#else> <p>没有查询到符合条件的结果,请更换条件再试试。</p> </#if> <table style="text-align: center;" width="100%" id="mytable"> <tr> <td colspan="12" height="30" align="center"><span class="STYLE1">用户列表</span></td> </tr> <tr> <td width="100" height="30" align="center" valign="middle"><span class="STYLE15">提现流水号</span></td> <td width="100" height="30" align="center" valign="middle"><span class="STYLE15">提现用户账号</span></td> <td width="100" height="30" align="center" valign="middle"><span class="STYLE15">账户余额(¥)</span></td> <td width="100" height="30" align="center" valign="middle"><span class="STYLE15">提现金额(¥)</span></td> <td width="100" height="30" align="center" valign="middle"><span class="STYLE15">提现银行</span></td> <td width="100" height="30" align="center" valign="middle"><span class="STYLE15">银行卡号</span></td> <td width="100" height="30" align="center" valign="middle"><span class="STYLE15">提现申请时间</span></td> <td width="100" height="30" align="center" valign="middle"><span class="STYLE15">提现状态</span></td> <td width="100" height="30" align="center" valign="middle"><span class="STYLE15">创建时间</span></td> <td width="100" height="30" align="center" valign="middle"><span class="STYLE15">更新时间</span></td> <td colspan="2" width="100" height="30" align="center" valign="middle"><span class="STYLE15">功能</span></td> </tr> <div id="detail"> </div> </table> <br /> <div align="center" id="info"> <a href="clear">注销</a> </div> <script type="text/javascript"> /** * ajax-----提交的参数可以通过url提交,也可以用data:{}方式提交 */ function setshow() { //$.ajax({type: "POST",url:"user/login",async:false,data: params}); /** data : { 'room' : $("#roomid").find('option:selected').text(), 'roomid' :$("#roomid").val() }, $.each(params, function(i, field) { $("#results").append( field.name + ":" + field.value + "<br />"); if (isEmpty(field.value)) { return;//验证是否为空 } }); <#if loginStatus??> loginStatus+='${loginStatus}'; </#if> $("#loginForm").attr("action", "user/login"); $("#loginForm").submit(); if(status == "success" && !isEmpty(loginStatus) && loginStatus !="用户名或密码错误"){ document.write(data); window.location.href="user/userList"; }*/ // params = $("#inform").serializeArray(); var jsonObjInfo = $.toJSON($('#inform').serializeObject()); alert("jsonObjInfo "+jsonObjInfo); jQuery.ajax( { type : "POST", contentType : 'text/html', url : "list", data : jsonObjInfo, dataType : "json", success : function(data) { if (data && data.success == "true") { var num = 0; var tableHtml =""; $.each(data.data, function(i, item) { tableHtml+= "<tr>" +"<td width='100' height='30' align='center' valign='middle'><span" +"class='STYLE15'>"+item.iAutoID+"</span></td>" +"<td width='100' height='30' align='center' valign='middle'><span" +"class='STYLE15'>"+item.sMobile+"</span></td>" +"<td width='100' height='30' align='center' valign='middle'><span" +"class='STYLE15'>"+item.iAmount+"</span></td>" +"<td width='100' height='30' align='center' valign='middle'><span" +"class='STYLE15'>"+item.iMoney+"</span></td>" +"<td width='100' height='30' align='center' valign='middle'><span" +"class='STYLE15'>"+item.sBankName+"</span></td>" +"<td width='100' height='30' align='center' valign='middle'><span" +"class='STYLE15'>"+item.sBankCard+"</span></td>" +"<td width='100' height='30' align='center' valign='middle'><span" +"class='STYLE15'>"+item.iGetCashTime+"</span></td>" +"<td width='100' height='30' align='center' valign='middle'><span" +"class='STYLE15'>"+item.iStatus+"</span></td>" +"<td width='100' height='30' align='center' valign='middle'><span" +"class='STYLE15'>"+item.iCreateTime+"</span></td>" +"<td width='100' height='30' align='center' valign='middle'><span" +"class='STYLE15'>"+item.iUpdateTime+"</span></td>" +"<td width='50' height='30' align='center' valign='middle'>" +"<a onclick='removeTr('"+item.iAutoID+"')>删除</a></td>" +"<td width='50' height='30' align='center' valign='middle'>" +"<a id='save' href=deleteUser?id='"+item.iAutoID+"'>保存</a></td>" +"<td width='50' height='30' align='center' valign='middle'>" +"<a href=findUser?id="+item.iAutoID+">修改</a></td>" +"<td width='50' height='30' align='center' valign='middle'>" +"<a href=deleteUser?id="+item.iAutoID+">删除</a></td>" +"<td width='50' height='30' align='center' valign='middle'>" +"<a href=showItem("+item.iAutoID+")>详细信息</a></td>" +"</tr>"; $('#info').append( "提现流水号:"+ +"提现用户账号:"+item.sMobile +"账户余额(¥):"+item.iAmount +"提现金额(¥):"+item.iMoney+" " +"提现银行:"+item.sBankName +"银行卡号:"+item.sBankCard +"提现申请时间:"+item.iGetCashTime+" " +"提现状态:"+item.iStatus +"创建时间:"+item.iCreateTime +"更新时间:"+item.iUpdateTime+" "); }); var elements = $("#mytable").children().length; //表示id为“mytable”的标签下的子标签的个数 $("#mytable").children().eq(elements - 1).after(tableHtml); //在表头之后添加空白行 num++; //$('#detail').html(tableHtml); alert("返回数据成功!"); } else { alert("返回数据失败!"); } }, error : function() { alert("网络连接出错!"); } }); } function showItem(id) { $.ajax({ type : "GET", url : "${base}/findLoan", data :{'id' : id}, dataType : "json", success : function(data) { if (data && data.success == "true") { $.each(data.data, function(i, item) { $('#info').append( "提现流水号:"+item.iAutoID +"提现用户账号:"+item.sMobile +"账户余额(¥):"+item.iAmount +"提现金额(¥):"+item.iMoney+" " +"提现银行:"+item.sBankName +"银行卡号:"+item.sBankCard +"提现申请时间:"+item.iGetCashTime+" " +"提现状态:"+item.iStatus +"创建时间:"+item.iCreateTime +"更新时间:"+item.iUpdateTime+" "); }); alert("返回数据成功!"); } else { alert("返回数据失败!"); } }, error : function() { alert("网络连接出错!"); } }); } //非空判断 function isEmpty(strIn) { if (strIn === undefined) { return true; } else if (strIn == null) { return true; } else if (strIn == "") { return true; } else { return false; } } /* function check(obj) { var content = obj.val(); var attribute = obj.attr("name"); if (attribute == "account" && !content) {//验证用户名是否为空 $("#msg1").val("用户名不能为空!"); $("#account").focus(); } else { $("#msg1").val(""); } if (attribute == "passwd" && !content) {//验证密码是否为空 $("#msg2").val("密码不能为空!"); $("#passwd").focus(); } else { $("#msg2").val(""); } } */ </script> </body> </html>
<!-- json转换 ,springmvc 返回json格式必须加入 --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.3</version> </dependency>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射,请求映射 --> <context:annotation-config /> <context:component-scan base-package="com.test.action" /> <!-- 启动基于Spring MVC的注解功能,激活@Controller,将控制器与方法映射加入到容器中 --> <!--避免IE执行AJAX时,返回JSON出现下载文件 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
在 Spring mvc3中,响应、接受 JSON都十分方便。
使用注解@ResponseBody可以将结果(一个包含字符串和JavaBean的Map),转换成JSON。
使用 @RequestBody 注解前台只需要向 Controller 提交一段符合格式的 JSON,Spring 会自动将其拼装成 bean。
Spring这个转换是靠org.codehaus.jackson这个组件来实现的,所以需要引入jackson-core-asl和org.codehaus.jackson两个jar包。