同样的代码导入后运行结果不一样

沧海飞尘 2017-07-06 10:12:23
jsp异步获得一个Product,里面的name属性正常获得,id属性却显示undefined,为什么同样的代码老师环境能正常获得属性,导入到我的电脑上就不行?
...全文
254 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
沧海飞尘 2017-07-07
  • 打赏
  • 举报
回复
引用 16 楼 pany1209 的回复:
[quote=引用 14 楼 沧海飞尘的回复:][quote=引用 13 楼 pany1209 的回复:] [quote=引用 11 楼 沧海飞尘的回复:][quote=引用 10 楼 pany1209 的回复:] [quote=引用 4 楼 qq_37715200 的回复:] [quote=引用 3 楼 pany1209 的回复:] 所以代码呢???
这是JSP的上的代码: <script type="text/javascript"> $(function(){ var content = ""; $.post( "${pageContext.request.contextPath}/findCategory", function(data){ for(var i=0;i<data.length;i++){ //alert(data[i].cname+data[i].cid); content+= "<li><a href='${pageContext.request.contextPath}/productListByCid?cid="+data[i].cid+"'>"+data[i].cname+"</a></li>"; } $("#categoryUL").html(content); }, "json" ); }); </script> 这是findCategory的代码: ProductService service = new ProductService(); Jedis jedis = JedisPoolUtils.getJedis(); String categoryListJson = jedis.get("categoryListJson"); if(categoryListJson==null){ // 获得商品分类 List<Category> categoryList = service.findAllCategory(); Gson gson = new Gson(); categoryListJson = gson.toJson(categoryList); jedis.set("categoryListJson", categoryListJson); } response.setContentType("text/heml;charset=UTF-8"); response.getWriter().write(categoryListJson);[/quote] 打印一下categoryListJson是什么样的???[/quote] 控制台显示: [{"cname":"手机数码"},{"cname":"运动户外"},{"cname":"电脑办公"},{"cname":"家具家居"},{"cname":"鞋靴箱包"},{"cname":"图书音像"},{"cname":"母婴孕婴"},{"cname":"汽车用品"}] 打印list的getId也是空[/quote] 你这个json数据只有cname,肯定显示不出id。。。[/quote] dao层就一句select,不懂为什么获得不了,我在cmd上能正常获得 [/quote] 把redis的缓存删掉试试。。。。直接查数据库,打印出来json是否有id???[/quote] 厉害了。。清了一下redis的缓存,数据读出来了, 原来是没清缓存,初学者还真是容易犯细节上的错误啊....
李德胜1995 2017-07-07
  • 打赏
  • 举报
回复
引用 14 楼 沧海飞尘的回复:
[quote=引用 13 楼 pany1209 的回复:] [quote=引用 11 楼 沧海飞尘的回复:][quote=引用 10 楼 pany1209 的回复:] [quote=引用 4 楼 qq_37715200 的回复:] [quote=引用 3 楼 pany1209 的回复:] 所以代码呢???
这是JSP的上的代码: <script type="text/javascript"> $(function(){ var content = ""; $.post( "${pageContext.request.contextPath}/findCategory", function(data){ for(var i=0;i<data.length;i++){ //alert(data[i].cname+data[i].cid); content+= "<li><a href='${pageContext.request.contextPath}/productListByCid?cid="+data[i].cid+"'>"+data[i].cname+"</a></li>"; } $("#categoryUL").html(content); }, "json" ); }); </script> 这是findCategory的代码: ProductService service = new ProductService(); Jedis jedis = JedisPoolUtils.getJedis(); String categoryListJson = jedis.get("categoryListJson"); if(categoryListJson==null){ // 获得商品分类 List<Category> categoryList = service.findAllCategory(); Gson gson = new Gson(); categoryListJson = gson.toJson(categoryList); jedis.set("categoryListJson", categoryListJson); } response.setContentType("text/heml;charset=UTF-8"); response.getWriter().write(categoryListJson);[/quote] 打印一下categoryListJson是什么样的???[/quote] 控制台显示: [{"cname":"手机数码"},{"cname":"运动户外"},{"cname":"电脑办公"},{"cname":"家具家居"},{"cname":"鞋靴箱包"},{"cname":"图书音像"},{"cname":"母婴孕婴"},{"cname":"汽车用品"}] 打印list的getId也是空[/quote] 你这个json数据只有cname,肯定显示不出id。。。[/quote] dao层就一句select,不懂为什么获得不了,我在cmd上能正常获得 [/quote] 不知道你是怎么查的,反正打印出来的json没有id在页面肯定是undefined。。。
李德胜1995 2017-07-07
  • 打赏
  • 举报
回复
引用 14 楼 沧海飞尘的回复:
[quote=引用 13 楼 pany1209 的回复:] [quote=引用 11 楼 沧海飞尘的回复:][quote=引用 10 楼 pany1209 的回复:] [quote=引用 4 楼 qq_37715200 的回复:] [quote=引用 3 楼 pany1209 的回复:] 所以代码呢???
这是JSP的上的代码: <script type="text/javascript"> $(function(){ var content = ""; $.post( "${pageContext.request.contextPath}/findCategory", function(data){ for(var i=0;i<data.length;i++){ //alert(data[i].cname+data[i].cid); content+= "<li><a href='${pageContext.request.contextPath}/productListByCid?cid="+data[i].cid+"'>"+data[i].cname+"</a></li>"; } $("#categoryUL").html(content); }, "json" ); }); </script> 这是findCategory的代码: ProductService service = new ProductService(); Jedis jedis = JedisPoolUtils.getJedis(); String categoryListJson = jedis.get("categoryListJson"); if(categoryListJson==null){ // 获得商品分类 List<Category> categoryList = service.findAllCategory(); Gson gson = new Gson(); categoryListJson = gson.toJson(categoryList); jedis.set("categoryListJson", categoryListJson); } response.setContentType("text/heml;charset=UTF-8"); response.getWriter().write(categoryListJson);[/quote] 打印一下categoryListJson是什么样的???[/quote] 控制台显示: [{"cname":"手机数码"},{"cname":"运动户外"},{"cname":"电脑办公"},{"cname":"家具家居"},{"cname":"鞋靴箱包"},{"cname":"图书音像"},{"cname":"母婴孕婴"},{"cname":"汽车用品"}] 打印list的getId也是空[/quote] 你这个json数据只有cname,肯定显示不出id。。。[/quote] dao层就一句select,不懂为什么获得不了,我在cmd上能正常获得 [/quote] 把redis的缓存删掉试试。。。。直接查数据库,打印出来json是否有id???
沧海飞尘 2017-07-07
  • 打赏
  • 举报
回复
引用 12 楼 LoginandPwd 的回复:
运行环境可以不一样吧,比如jdk,tomcat等
老师的jdk是1.7.0_72,我的是_17。tomcat用的跟他一样,还有eclipse版本貌似比他的高
沧海飞尘 2017-07-07
  • 打赏
  • 举报
回复
引用 13 楼 pany1209 的回复:
[quote=引用 11 楼 沧海飞尘的回复:][quote=引用 10 楼 pany1209 的回复:] [quote=引用 4 楼 qq_37715200 的回复:] [quote=引用 3 楼 pany1209 的回复:] 所以代码呢???
这是JSP的上的代码: <script type="text/javascript"> $(function(){ var content = ""; $.post( "${pageContext.request.contextPath}/findCategory", function(data){ for(var i=0;i<data.length;i++){ //alert(data[i].cname+data[i].cid); content+= "<li><a href='${pageContext.request.contextPath}/productListByCid?cid="+data[i].cid+"'>"+data[i].cname+"</a></li>"; } $("#categoryUL").html(content); }, "json" ); }); </script> 这是findCategory的代码: ProductService service = new ProductService(); Jedis jedis = JedisPoolUtils.getJedis(); String categoryListJson = jedis.get("categoryListJson"); if(categoryListJson==null){ // 获得商品分类 List<Category> categoryList = service.findAllCategory(); Gson gson = new Gson(); categoryListJson = gson.toJson(categoryList); jedis.set("categoryListJson", categoryListJson); } response.setContentType("text/heml;charset=UTF-8"); response.getWriter().write(categoryListJson);[/quote] 打印一下categoryListJson是什么样的???[/quote] 控制台显示: [{"cname":"手机数码"},{"cname":"运动户外"},{"cname":"电脑办公"},{"cname":"家具家居"},{"cname":"鞋靴箱包"},{"cname":"图书音像"},{"cname":"母婴孕婴"},{"cname":"汽车用品"}] 打印list的getId也是空[/quote] 你这个json数据只有cname,肯定显示不出id。。。[/quote] dao层就一句select,不懂为什么获得不了,我在cmd上能正常获得
李德胜1995 2017-07-07
  • 打赏
  • 举报
回复
引用 11 楼 沧海飞尘的回复:
[quote=引用 10 楼 pany1209 的回复:] [quote=引用 4 楼 qq_37715200 的回复:] [quote=引用 3 楼 pany1209 的回复:] 所以代码呢???
这是JSP的上的代码: <script type="text/javascript"> $(function(){ var content = ""; $.post( "${pageContext.request.contextPath}/findCategory", function(data){ for(var i=0;i<data.length;i++){ //alert(data[i].cname+data[i].cid); content+= "<li><a href='${pageContext.request.contextPath}/productListByCid?cid="+data[i].cid+"'>"+data[i].cname+"</a></li>"; } $("#categoryUL").html(content); }, "json" ); }); </script> 这是findCategory的代码: ProductService service = new ProductService(); Jedis jedis = JedisPoolUtils.getJedis(); String categoryListJson = jedis.get("categoryListJson"); if(categoryListJson==null){ // 获得商品分类 List<Category> categoryList = service.findAllCategory(); Gson gson = new Gson(); categoryListJson = gson.toJson(categoryList); jedis.set("categoryListJson", categoryListJson); } response.setContentType("text/heml;charset=UTF-8"); response.getWriter().write(categoryListJson);[/quote] 打印一下categoryListJson是什么样的???[/quote] 控制台显示: [{"cname":"手机数码"},{"cname":"运动户外"},{"cname":"电脑办公"},{"cname":"家具家居"},{"cname":"鞋靴箱包"},{"cname":"图书音像"},{"cname":"母婴孕婴"},{"cname":"汽车用品"}] 打印list的getId也是空[/quote] 你这个json数据只有cname,肯定显示不出id。。。
mcg-helper 2017-07-07
  • 打赏
  • 举报
回复
运行环境可以不一样吧,比如jdk,tomcat等
沧海飞尘 2017-07-07
  • 打赏
  • 举报
回复
引用 10 楼 pany1209 的回复:
[quote=引用 4 楼 qq_37715200 的回复:] [quote=引用 3 楼 pany1209 的回复:] 所以代码呢???
这是JSP的上的代码: <script type="text/javascript"> $(function(){ var content = ""; $.post( "${pageContext.request.contextPath}/findCategory", function(data){ for(var i=0;i<data.length;i++){ //alert(data[i].cname+data[i].cid); content+= "<li><a href='${pageContext.request.contextPath}/productListByCid?cid="+data[i].cid+"'>"+data[i].cname+"</a></li>"; } $("#categoryUL").html(content); }, "json" ); }); </script> 这是findCategory的代码: ProductService service = new ProductService(); Jedis jedis = JedisPoolUtils.getJedis(); String categoryListJson = jedis.get("categoryListJson"); if(categoryListJson==null){ // 获得商品分类 List<Category> categoryList = service.findAllCategory(); Gson gson = new Gson(); categoryListJson = gson.toJson(categoryList); jedis.set("categoryListJson", categoryListJson); } response.setContentType("text/heml;charset=UTF-8"); response.getWriter().write(categoryListJson);[/quote] 打印一下categoryListJson是什么样的???[/quote] 控制台显示: [{"cname":"手机数码"},{"cname":"运动户外"},{"cname":"电脑办公"},{"cname":"家具家居"},{"cname":"鞋靴箱包"},{"cname":"图书音像"},{"cname":"母婴孕婴"},{"cname":"汽车用品"}] 打印list的getId也是空
李德胜1995 2017-07-06
  • 打赏
  • 举报
回复
引用 4 楼 qq_37715200 的回复:
[quote=引用 3 楼 pany1209 的回复:] 所以代码呢???
这是JSP的上的代码: <script type="text/javascript"> $(function(){ var content = ""; $.post( "${pageContext.request.contextPath}/findCategory", function(data){ for(var i=0;i<data.length;i++){ //alert(data[i].cname+data[i].cid); content+= "<li><a href='${pageContext.request.contextPath}/productListByCid?cid="+data[i].cid+"'>"+data[i].cname+"</a></li>"; } $("#categoryUL").html(content); }, "json" ); }); </script> 这是findCategory的代码: ProductService service = new ProductService(); Jedis jedis = JedisPoolUtils.getJedis(); String categoryListJson = jedis.get("categoryListJson"); if(categoryListJson==null){ // 获得商品分类 List<Category> categoryList = service.findAllCategory(); Gson gson = new Gson(); categoryListJson = gson.toJson(categoryList); jedis.set("categoryListJson", categoryListJson); } response.setContentType("text/heml;charset=UTF-8"); response.getWriter().write(categoryListJson);[/quote] 打印一下categoryListJson是什么样的???
沧海飞尘 2017-07-06
  • 打赏
  • 举报
回复
难道是tomcat部署web应用时候出错了?
沧海飞尘 2017-07-06
  • 打赏
  • 举报
回复
我直接导入视频上那个源码,运行时候也是不能获得id
沧海飞尘 2017-07-06
  • 打赏
  • 举报
回复
引用 6楼NANU-NANA 的回复:
你定义id属性了么?
实体类属性错误这个情况我考虑到了,没有错
NANU-NANA 2017-07-06
  • 打赏
  • 举报
回复
你定义id属性了么?
沧海飞尘 2017-07-06
  • 打赏
  • 举报
回复
引用 2楼ZHOU西口 的回复:
别怀疑人生,继续怀疑你代码
代码我贴出来了,帮忙看一下呗
沧海飞尘 2017-07-06
  • 打赏
  • 举报
回复
引用 3 楼 pany1209 的回复:
所以代码呢???
这是JSP的上的代码: <script type="text/javascript"> $(function(){ var content = ""; $.post( "${pageContext.request.contextPath}/findCategory", function(data){ for(var i=0;i<data.length;i++){ //alert(data[i].cname+data[i].cid); content+= "<li><a href='${pageContext.request.contextPath}/productListByCid?cid="+data[i].cid+"'>"+data[i].cname+"</a></li>"; } $("#categoryUL").html(content); }, "json" ); }); </script> 这是findCategory的代码: ProductService service = new ProductService(); Jedis jedis = JedisPoolUtils.getJedis(); String categoryListJson = jedis.get("categoryListJson"); if(categoryListJson==null){ // 获得商品分类 List<Category> categoryList = service.findAllCategory(); Gson gson = new Gson(); categoryListJson = gson.toJson(categoryList); jedis.set("categoryListJson", categoryListJson); } response.setContentType("text/heml;charset=UTF-8"); response.getWriter().write(categoryListJson);
李德胜1995 2017-07-06
  • 打赏
  • 举报
回复
所以代码呢???
ZHOU西口 2017-07-06
  • 打赏
  • 举报
回复
别怀疑人生,继续怀疑你代码
沧海飞尘 2017-07-06
  • 打赏
  • 举报
回复
这两天快被这问题搞得怀疑人生了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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