SpringBoot中访问控制层(controller)得不到Json数据

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_40709110/article/details/91655427

SpringBoot中访问控制层(controller)得不到Json数据

Controller层代码

在这里插入图片描述

application.yml配置文件代码

在这里插入图片描述
此时访问控制层的URL为:
localhost:8080/sell/buyer/product/List
在这里插入图片描述

开始使用spring boot的时候没有注意这个属性,其实默认可以不配置,直接在controller层通过@RequestMapping来设定url的地址路径。

如果server.context-path没有配,请求的url地址就是 localhost : 8080/buyer/product/List

一开始出现访问不到控制层的json数据,以为是端口号出现错误,浏览index.html页面
在这里插入图片描述
发现端口号为63342端口,可把我惊奇了,更改了端口还是没能解决问题;
在这里插入图片描述
又以为,是启动springboot的启动文件问题,没有进行注解的扫描,添加了@ComponentScan注解,还是不对。。。。
在这里插入图片描述
最后在URL地址上发现配置了server.context-path文件(/sell),所以进行访问的路径为:localhost:8080/sell/控制层的映射地址。解决问题

展开阅读全文

GridPanel得不到Json数据,

08-12

思路rngetuser.jsp -> submit -> redirectuserlist.action -> userlist.jsp -> searchuser.action -> userlist.jsp(GridPanel显示数据)rnrnStruts.xml 相关rn[code="xml"]rnrn /userlist.jsprn rn rn /userlist.jsprn rn[/code]rnrn相关action的代码rn[code="java"]rnpublic String searchUser() rnrn try rn ActionContext ctx = ActionContext.getContext(); rn HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); rn HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE); rn response.reset(); rn response.setCharacterEncoding("utf-8"); rn ComUserInfo comuserinfo = new ComUserInfo();rn if (comuserinfo.getUserid() != "") rn List result = uo.searchUser(getUserid());rn JsonUtil ju = new JsonUtil();rn String jsresult = ju.list2json(result);rn this.userlist = ("\"totalCount\": 3," + "\"userlist:\" " + jsresult + " ");rn System.out.println(userlist);rn response.getWriter().write(userlist);rn //response.getWriter().println(userlist);rn return SUCCESS;rn else rn ssn.add("errmsg", "User not login yet!");rn return ERROR;rn rn catch (Exception e) rn ssn.add("errmsg", e.getMessage());rn return ERROR;rn rn rn rn public String redirectUserList()rn return SUCCESS;rn rnrn[/code]rnrnuserlist.jsp上的 js 代码:rnrn[code="java"]rnrnExt.onReady(function() rn Ext.QuickTips.init();rnrn // turn on validation errors beside the field globallyrn // Ext.form.Field.prototype.msgTarget = 'side';rn var cm = new Ext.grid.ColumnModel([rn header : '用户ID',rn dataIndex : 'userId'rn , rn header : '用户昵称',rn dataIndex : 'usrName'rn , rn header : '密码',rn dataIndex : 'usrPwd'rn , rn header : '电子邮件地址',rn dataIndex : 'usrEmail'rn , rn header : '密码提示问题',rn dataIndex : 'usrPwdQues'rn , rn header : '密码提示问题答案',rn dataIndex : 'usrPwdAns'rn ]);rnrn var reader = new Ext.data.JsonReader(rn root : 'userlist',rn totalProperty: 'totalCount'rnrn , [rn name : 'usrId',rn mapping : 'userId'rn , rn name : 'usrName',rn mapping : 'usrName'rn , rn name : 'usrPwd',rn mapping : 'usrPwd'rn , rn name : 'usrEmail',rn mapping : 'usrEmail'rn , rn name : 'usrPwdQues',rn mapping : 'usrPwdQues'rn , rn name : 'usrPwdAns',rn mapping : 'usrPwdAns'rn ]);rnrn var ds = new Ext.data.Store(rn proxy : new Ext.data.HttpProxy(rn url : 'searchuser.action',rn method : 'POST'rn //method : 'GET'rn ),rn reader : reader,rn autoload : truern ); rn// ds.load();rn// alert(ds.getCount()) ; rn //var sm = new Ext.grid.CheckboxSelectionModel();rnrn var grid = new Ext.grid.GridPanel(rn store : ds,rn // sm : sm,rn cm : cm,rn loadMask : rn msg : ' 数据加载中...'rn ,rn autoHeight : truernrn );rnrn var vp = new Ext.Viewport(rn layout : "border",rn frame : true,rn title : "用户列表",rn autoHeight : true,rn items : [// Viewportrn region : "north",rn height : 100,rn title : "顶部面板"rn , rn region : "south",rn height : 50,rn title : "底部面板"rn , rn region : "west",rn width : "20%",rn title : "左边面板"rn , rn region : "east",rn width : "20%",rn title : "右边面板"rn , rn region : "center",rn title : "中央面板",rn items : [frm = new Ext.FormPanel(rn items : [grid]rn /* Eof formpanel */)rn /* Eof border center */]rn /* Eof Viewport */]rn );rn grid.render();rnrn vp.render(document.body);rn);rn[/code]rnrnrnaction返回的数据如下:rnrn"totalCount": 3,"userlist:" [rn"usrCreateBy":"","usrCreateDate":"","usrDisableDate":"",rn"usrEmail":"Frankie@together.com","usrId":"80319474","usrName":"Frankie","usrPwd":"1233","usrPwdAns":"1+1","usrPwdQues":"2",rn"usrRemark":"","usrUpdateBy":"","usrUpdateDate":""rn,rn"usrCreateBy":"","usrCreateDate":"","usrDisableDate":"",rn"usrEmail":"Justin@together.com","usrId":"80334272","usrName":"Justin","usrPwd":"1234","usrPwdAns":"2","usrPwdQues":"1+1",rn"usrRemark":"nothing","usrUpdateBy":"","usrUpdateDate":""rn,rn"usrCreateBy":"","usrCreateDate":"","usrDisableDate":"",rn"usrEmail":"Justin@together.com","usrId":"sa","usrName":"sysadmin","usrPwd":"a","usrPwdAns":"2","usrPwdQues":"1+1",rn"usrRemark":"nothing","usrUpdateBy":"","usrUpdateDate":""rnrn]rnrnrn困扰好几天了,请大家帮忙看看了,谢谢了!n[b]问题补充:[/b]n感谢atian25 的回答! rn利用你的方法,我已经可以确定json是没有问题了.利用var json=......; ds.loaddata(json);我可以在GridPanel里看到数据.rn但是还是没办法获得searchuser.action返回的数据.我想问题可能出在public String searchUser()... 和 rnvar ds = new Ext.data.Store( rn proxy : new Ext.data.HttpProxy( rn url : 'searchuser.action', rn method : 'POST' rn //method : 'GET' rn ), rn reader : reader, rn autoload : true rn ); rn之间.我想请问,我的searchUser()方法和ds是否有问题?比如searchUser()是否一定要用response.getWriter().println(userlist);将数据打印出来,或者ds里的 method : 'POST' or method : 'GET' 会影响?rn rnn[b]问题补充:[/b]n[code="java"][/code][code="java"]rnpublic String searchUser() rn try rn ActionContext ctx = ActionContext.getContext(); rn HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); rn HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE); rn response.reset(); rn response.setCharacterEncoding("utf-8"); rn ComUserInfo comuserinfo = new ComUserInfo(); rn if (comuserinfo.getUserid() != "") rn List result = uo.searchUser(getUserid()); rn JsonUtil ju = new JsonUtil(); rn String jsresult = ju.list2json(result); rn this.userlist = ("\"totalCount\": 3," + "\"userlist:\" " + jsresult + " "); rn System.out.println(userlist); rn response.getWriter().write(userlist); rn //response.getWriter().println(userlist); rn return SUCCESS; rn else rn ssn.add("errmsg", "User not login yet!"); rn return ERROR; rn rn catch (Exception e) rn ssn.add("errmsg", e.getMessage()); rn return ERROR; rn rn rn rn public String redirectUserList() rn return SUCCESS; rn rn[/code]rn打印出来的数据是:rn"totalcount":1,"userlist": ["usrCreateBy":"","usrCreateDate":"","usrDisableDate":"","usrEmail":"Justin@together.com","usrId":"sa","usrName":"sysadmin","usrPwd":"a","usrPwdAns":"2","usrPwdQues":"1+1","usrRemark":"nothing","usrUpdateBy":"","usrUpdateDate":""] rn我将此数据放到js中:rnrn[code="java"]rn var json = rn "totalcount" : 1,rn "userlist" : [rn "usrCreateBy" : "",rn "usrCreateDate" : "",rn "usrDisableDate" : "",rn "usrEmail" : "Justin@together.com",rn "usrId" : "sa",rn "usrName" : "sysadmin",rn "usrPwd" : "a",rn "usrPwdAns" : "2",rn "usrPwdQues" : "1+1",rn "usrRemark" : "nothing",rn "usrUpdateBy" : "",rn "usrUpdateDate" : ""rn ]rn rnrn// ds.load();rn ds.loadData(json);rn[/code]rn可以成功显示,但是用ds.load();去获得action返回的时候却失败了.实在搞不明白.rnrnrnrnn[b]问题补充:[/b]n公司没办法装东西,看了下别人的,把代码简化了下,直接给页面打印字符串:rn[code="java"]rnpublic String searchUser() rn try rn ComUserInfo comuserinfo = new ComUserInfo();rn if (comuserinfo.getUserid() != "") rn List result = uo.searchUser(getUserid());rn String totalcount = tc.intToString(result.size());rn JsonUtil ju = new JsonUtil();rn String jsresult = ju.list2json(result);rn setUserlist("\"totalcount\" : \"1\",\"userlist\" : ["rn + "\"usrId\" : \"sa\","rn + "\"usrName\" : \"sysadmin\"]" + "");rn System.out.println(userlist);rn return SUCCESS;rn else rn ssn.add("errmsg", "User not login yet!");rn return ERROR;rn rn catch (Exception e) rn ssn.add("errmsg", e.getMessage());rn return ERROR;rn rn rn[/code]rnrnjs代码:rn[code="java"]rn/*rn * Ext JS Library 3.0 RC2 Copyright(c) 2006-2009, Ext JS, LLC.rn * licensing@extjs.comrn * rn * http://extjs.com/licensern */rnrnExt.onReady(function() rn Ext.QuickTips.init();rnrn // turn on validation errors beside the field globallyrn // Ext.form.Field.prototype.msgTarget = 'side';rn var cm = new Ext.grid.ColumnModel([rn header : '用户ID',rn dataIndex : 'usrId'rn , rn header : '用户昵称',rn dataIndex : 'usrName'rn , rn header : '密码',rn dataIndex : 'usrPwd'rn , rn header : '电子邮件地址',rn dataIndex : 'usrEmail'rn , rn header : '密码提示问题',rn dataIndex : 'usrPwdQues'rn , rn header : '密码提示问题答案',rn dataIndex : 'usrPwdAns'rn ]);rnrn var reader = new Ext.data.JsonReader(rn root : 'userlist',rn totalProperty : 'totalcount'rnrn , [rn name : 'usrId',rn mapping : 'usrId'rn , rn name : 'usrName',rn mapping : 'usrName'rn , rn name : 'usrPwd',rn mapping : 'usrPwd'rn , rn name : 'usrEmail',rn mapping : 'usrEmail'rn , rn name : 'usrPwdQues',rn mapping : 'usrPwdQues'rn , rn name : 'usrPwdAns',rn mapping : 'usrPwdAns'rn ]);rnrn var ds = new Ext.data.Store(rn proxy : new Ext.data.HttpProxy(rn url : 'searchuser.action'rn // method : 'POST'rn // method : 'GET'rn ),rn reader : readerrnrn );rnrn// var json ="totalcount" : "1","userlist" : ["usrId" : "sa","usrName" : "sysadmin"]rnrn ds.load();rn//ds.loadData(json);rn alert(ds.getCount());rn // var sm = new Ext.grid.CheckboxSelectionModel();rnrn var grid = new Ext.grid.GridPanel(rn store : ds,rn // sm : sm,rn cm : cm,rn loadMask : rn msg : ' 数据加载中...'rn ,rn autoHeight : truernrn );rnrn var vp = new Ext.Viewport(rn layout : "border",rn frame : true,rn title : "用户列表",rn autoHeight : true,rn items : [// Viewportrn region : "north",rn height : 100,rn title : "顶部面板"rn , rn region : "south",rn height : 50,rn title : "底部面板"rn , rn region : "west",rn width : "20%",rn title : "左边面板"rn , rn region : "east",rn width : "20%",rn title : "右边面板"rn , rn region : "center",rn title : "中央面板",rn items : [frm = new Ext.FormPanel(rn items : [grid]rn /* Eof formpanel */)rn /* Eof border center */]rn /* Eof Viewport */]rn );rn grid.render();rnrn vp.render(document.body);rn);rn[/code]rn直接访问"searchuser.action"rn可以得到rn"totalcount" : "1","userlist" : ["usrId" : "sa","usrName" : "sysadmin"] rnrn似乎没有多余数据.rnn[b]问题补充:[/b]n增加一个页面ul.jsp,里面放[code="xml"] 123[/code]能获得数据rn"totalcount" : "1","userlist" : ["usrId" : "sa","usrName" : "sysadmin"] rnrnjs里面修改urlrn[code="java"]rn var ds = new Ext.data.Store(rn proxy : new Ext.data.HttpProxy(rn url : 'ul.jsp'rn ),rn reader : readerrn );rn[/code]rn然后直接点123访问"userlist.jsp" 还是得不到数据.rnrnrnn[b]问题补充:[/b]n得到数据了,增加了一个ul.jsp,然后把数据打印给它,在ul.jsp内用javascript rn[code="java"]rn ValueStack vs = (ValueStack) request.getAttribute("struts.valueStack");rn String userlist = (String) vs.findValue("userlist"); rn String a = userlist;rn response.getWriter().write(a);rn[/code]rn获取数据并write, 然后把userlist.jsp的url指向ul.jsp,GridPanel就可以得到数据.rn似乎问题出在response上,action里面的数据返回时,并没有把数据放到response里面(action里的 response.getWriter().write(userlist);似乎无效).rn例如,ul.jsp是一完全空白页面,rn[code="java"]rnpublic String searchUser() rn try rn ActionContext ctx = ActionContext.getContext(); rn HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); rn HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE); rn response.reset(); rn response.setCharacterEncoding("utf-8"); rn ComUserInfo comuserinfo = new ComUserInfo();rn if (comuserinfo.getUserid() != "") rn List result = uo.searchUser(getUserid());rn String totalcount = tc.intToString(result.size());rn JsonUtil ju = new JsonUtil();rn String jsresult = ju.list2json(result);rn this.userlist =rn ("\"totalcount\":"+totalcount+",\"userlist\": " + jsresult +" ");rn System.out.println(userlist);rn response.getWriter().write(userlist);rn return SUCCESS;rn else rn ssn.add("errmsg", "User not login yet!");rn return ERROR;rn rn catch (Exception e) rn ssn.add("errmsg", e.getMessage());rn return ERROR;rn rn rn[/code]rn打印到ul.jsp的数据是空.rnn[b]问题补充:[/b]n这个思路 rngetuser.jsp -> submit -> redirectuserlist.action -> userlist.jsp -> searchuser.action -> userlist.jsp(GridPanel显示数据) rn不知道可行否,rn"userlist.jsp -> searchuser.action -> userlist.jsp(GridPanel显示数据)" 这里searchuser.action返回的数据userlist.jsp接受不到.rnn[b]问题补充:[/b]n似乎中间一定要有一个页面用于接收数据.rn以下方法可以成功在gridPanel上显示数据rnsearchuser.action->ul.jsp->userlist.jsp(js中url指向ul.jsp)rn不明白.rn为何 userlist.jsp->searchuser.action->userlist.jsp不行... 问答

没有更多推荐了,返回首页