请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

getshopbyid不能返回数据显示在前端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//ShopManagementController
@RequestMapping(value="/getshopbyid",method=RequestMethod.GET)
    @ResponseBody
    private Map<String,Object> getShopById(HttpServletRequest request){
        Map<String,Object> modelMap=new HashMap<String,Object>();
        Long shopId=HttpServletRequestUtil.getLong(request, "shopId");
        if(shopId>-1){
            try{
                Shop shop=shopService.getByShopId(shopId);
                List<Area> areaList=areaService.getAreaList();
                modelMap.put("shop", shop);
                modelMap.put("areaList", areaList);
                modelMap.put("success"true); 
            }catch(Exception e){
                 
                modelMap.put("success",false);
                modelMap.put("errMsg",e.getMessage());
            }
        }else{
            modelMap.put("success",false);
            modelMap.put("errMsg""empty shopId");
        }
        return modelMap;
    }
     
     
    //shopoperation.js
    function getShopInfo(shopId){
        $.getJSON(shopInfoUrl,function(data){
            if(data.success){
                var shop=data.shop;
                $('#shop-name').val(shop.shopName);
                $('#shop-addr').val(shop.shopAddr);
                $('#shop-phone').val(shop.phone);
                $('#shop-info').val(shop.shopInfo);
                var shopCategory='<option data-id="'+shop.shopCategory.shopCategoryId+'"selected>'
                +shop.shopCategory.shopCategoryName+'</option>';
                var tempAreaHtml='';
                data.areaList.map(function(item,index){
                    //获取所有area,遍历
                    tempAreaHtml+='<option data-id="'+item.areaId+'">'
                    +item.areaName+'</option>';
                });
                $('#shop-category').html(shopCategory);
                $('#shop-category').attr('disabled','disabled');
                $('#area').html(tempAreaHtml);
                $('#area').attr('data-id',shop.areaId);
            }
        });
    }


正在回答 回答被采纳积分+3

插入代码

7回答

翔仔 2018-04-21 17:11:30

同学我这边把你的代码复制到我上面执行一点问题都没有,能展示出来的你再调试一下,确认后端是到success=true这行吗,是不是进入到false那边去了。因为我这边是没问题的,并且你可以看看spring-web.xml有什么问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解模式 -->
    <mvc:annotation-driven />
 
    <!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:default-servlet-handler />
 
    <!-- 3.定义视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/html/"></property>
        <property name="suffix" value=".html"></property>
    </bean>
    <!-- 文件上传解析器 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"></property>
        <!-- 1024 * 1024 * 20 = 20M -->
        <property name="maxUploadSize" value="20971520"></property>
        <property name="maxInMemorySize" value="20971520"></property>
    </bean>
 
    <!-- 4.扫描web相关的bean -->
    <context:component-scan base-package="com.imooc.o2o.web" />
     
</beans>

怀疑你这边是不是连redis的配置也复制了,如果redis报错是不会返回信息的

0 回复 有任何疑惑可以回复我~
  • 提问者 Cris_hu #1
    我这边的spring-web.xml配置和老师你的一样
    回复 有任何疑惑可以回复我~ 2018-04-21 17:22:57
  • 翔仔 回复 提问者 Cris_hu #2
    感觉你这方面能力一定要提升啊,肯定是哪里配置错了,没办法,你把项目打包成zip发到我邮箱 o2o_course@163.com
    回复 有任何疑惑可以回复我~ 2018-04-21 17:27:27
  • 翔仔 回复 提问者 Cris_hu #3
    下不为例,不可能天天帮同学定位问题的哈,一定要成长,一般这种是最不应该做的事情了
    回复 有任何疑惑可以回复我~ 2018-04-21 17:30:18
翔仔 2018-04-21 12:38:20

同学,我有点乱了,你到底是哪个方法传进去回不来,你就贴哪个方法以及对应的后台方法就好了。。。而且你ajax改的应该不是你问的方法。。。请描述问题的时候思维清晰一点哈

0 回复 有任何疑惑可以回复我~
  • 提问者 Cris_hu #1
    老师,真的真的很抱歉,代码已经重新编辑了。。。
    回复 有任何疑惑可以回复我~ 2018-04-21 16:45:06
  • 提问者 Cris_hu #2
    老师,我除了在写代码外,还在写学院的一个项目申报书,两点半的时候去提交资料,然后一阵困意袭来,回去休息了一个多小时。。。真的万分抱歉。。。我想老师请教一个小问题,您平时这么忙,一天有多少的睡眠时间呢,以及即使早起,如何能够保证一整天都精神饱满。。
    回复 有任何疑惑可以回复我~ 2018-04-21 16:48:58
  • 翔仔 回复 提问者 Cris_hu #3
    我精神也不饱满啊,所以得把事情处理得井井有条才可以,要有计划和效率,要换下思维去处理问题:)
    回复 有任何疑惑可以回复我~ 2018-04-21 17:13:25
翔仔 2018-04-21 11:58:06

shopoperation.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 
 */
$(function() {
    // 从URL里获取shopId参数的值
    var shopId = getQueryString('shopId');
    // 由于店铺注册和编辑使用的是同一个页面,
    // 该标识符用来标明本次是添加还是编辑操作
    var isEdit = shopId ? true false;
    // 用于店铺注册时候的店铺类别以及区域列表的初始化的URL
    var initUrl = '/o2o/shopadmin/getshopinitinfo';
    // 注册店铺的URL
    var registerShopUrl = '/o2o/shopadmin/registershop';
    // 编辑店铺前需要获取店铺信息,这里为获取当前店铺信息的URL
    var shopInfoUrl = "/o2o/shopadmin/getshopbyid?shopId=" + shopId;
    // 编辑店铺信息的URL
    var editShopUrl = '/o2o/shopadmin/modifyshop';
    // 判断是编辑操作还是注册操作
    if (!isEdit) {
        getShopInitInfo();
    else {
        getShopInfo(shopId);
    }
    // 通过店铺Id获取店铺信息
    function getShopInfo(shopId) {
        $.getJSON(shopInfoUrl, function(data) {
            if (data.success) {
                // 若访问成功,则依据后台传递过来的店铺信息为表单元素赋值
                var shop = data.shop;
                $('#shop-name').val(shop.shopName);
                $('#shop-addr').val(shop.shopAddr);
                $('#shop-phone').val(shop.phone);
                $('#shop-desc').val(shop.shopDesc);
                // 给店铺类别选定原先的店铺类别值
                var shopCategory = '<option data-id="'
                        + shop.shopCategory.shopCategoryId + '" selected>'
                        + shop.shopCategory.shopCategoryName + '</option>';
                var tempAreaHtml = '';
                // 初始化区域列表
                data.areaList.map(function(item, index) {
                    tempAreaHtml += '<option data-id="' + item.areaId + '">'
                            + item.areaName + '</option>';
                });
                $('#shop-category').html(shopCategory);
                // 不允许选择店铺类别
                $('#shop-category').attr('disabled''disabled');
                $('#area').html(tempAreaHtml);
                // 给店铺选定原先的所属的区域
                $("#area option[data-id='" + shop.area.areaId + "']").attr(
                        "selected""selected");
            }
        });
    }
    // 取得所有二级店铺类别以及区域信息,并分别赋值进类别列表以及区域列表
    function getShopInitInfo() {
        $.getJSON(initUrl, function(data) {
            if (data.success) {
                var tempHtml = '';
                var tempAreaHtml = '';
                data.shopCategoryList.map(function(item, index) {
                    tempHtml += '<option data-id="' + item.shopCategoryId
                            '">' + item.shopCategoryName + '</option>';
                });
                data.areaList.map(function(item, index) {
                    tempAreaHtml += '<option data-id="' + item.areaId + '">'
                            + item.areaName + '</option>';
                });
                $('#shop-category').html(tempHtml);
                $('#area').html(tempAreaHtml);
            }
        });
    }
    // 提交按钮的事件响应,分别对店铺注册和编辑操作做不同响应
    $('#submit').click(function() {
        // 创建shop对象
        var shop = {};
        if (isEdit) {
            // 若属于编辑,则给shopId赋值
            shop.shopId = shopId;
        }
        // 获取表单里的数据并填充进对应的店铺属性中
        shop.shopName = $('#shop-name').val();
        shop.shopAddr = $('#shop-addr').val();
        shop.phone = $('#shop-phone').val();
        shop.shopDesc = $('#shop-desc').val();
        // 选择选定好的店铺类别
        shop.shopCategory = {
            shopCategoryId : $('#shop-category').find('option').not(function() {
                return !this.selected;
            }).data('id')
        };
        // 选择选定好的区域信息
        shop.area = {
            areaId : $('#area').find('option').not(function() {
                return !this.selected;
            }).data('id')
        };
        // 获取上传的图片文件流
        var shopImg = $('#shop-img')[0].files[0];
        // 生成表单对象,用于接收参数并传递给后台
        var formData = new FormData();
        // 添加图片流进表单对象里
        formData.append('shopImg', shopImg);
        // 将shop json对象转成字符流保存至表单对象key为shopStr的的键值对里
        formData.append('shopStr', JSON.stringify(shop));
        // 获取表单里输入的验证码
        var verifyCodeActual = $('#j_captcha').val();
        if (!verifyCodeActual) {
            $.toast('请输入验证码!');
            return;
        }
        formData.append('verifyCodeActual', verifyCodeActual);
        // 将数据提交至后台处理相关操作
        $.ajax({
            url : (isEdit ? editShopUrl : registerShopUrl),
            type : 'POST',
            data : formData,
            contentType : false,
            processData : false,
            cache : false,
            success : function(data) {
                if (data.success) {
                    $.toast('提交成功!');
                    if (!isEdit) {
                        // 若为注册操作,成功后返回店铺列表页
                        window.location.href = "/o2o/shopadmin/shoplist";
                    }
                else {
                    $.toast('提交失败!' + data.errMsg);
                }
                // 点击验证码图片的时候,注册码会改变
                $('#captcha_img').click();
            }
        });
    });
 
})

ShopManagementController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package com.imooc.o2o.web.shopadmin;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.imooc.o2o.dto.ImageHolder;
import com.imooc.o2o.dto.ShopExecution;
import com.imooc.o2o.entity.Area;
import com.imooc.o2o.entity.PersonInfo;
import com.imooc.o2o.entity.Shop;
import com.imooc.o2o.entity.ShopCategory;
import com.imooc.o2o.enums.ShopStateEnum;
import com.imooc.o2o.exceptions.ShopOperationException;
import com.imooc.o2o.service.AreaService;
import com.imooc.o2o.service.ShopCategoryService;
import com.imooc.o2o.service.ShopService;
import com.imooc.o2o.util.CodeUtil;
import com.imooc.o2o.util.HttpServletRequestUtil;
 
@Controller
@RequestMapping("/shopadmin")
public class ShopManagementController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private ShopCategoryService shopCategoryService;
    @Autowired
    private AreaService areaService;
 
    @RequestMapping(value = "/getshopmanagementinfo", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> getShopManagementInfo(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        long shopId = HttpServletRequestUtil.getLong(request, "shopId");
        if (shopId <= 0) {
            Object currentShopObj = request.getSession().getAttribute("currentShop");
            if (currentShopObj == null) {
                modelMap.put("redirect"true);
                modelMap.put("url""/o2o/shopadmin/shoplist");
            else {
                Shop currentShop = (Shop) currentShopObj;
                modelMap.put("redirect"false);
                modelMap.put("shopId", currentShop.getShopId());
            }
        else {
            Shop currentShop = new Shop();
            currentShop.setShopId(shopId);
            request.getSession().setAttribute("currentShop", currentShop);
            modelMap.put("redirect"false);
        }
        return modelMap;
    }
 
    @RequestMapping(value = "/getshoplist", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> getShopList(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        PersonInfo user = (PersonInfo) request.getSession().getAttribute("user");
        try {
            Shop shopCondition = new Shop();
            shopCondition.setOwner(user);
            ShopExecution se = shopService.getShopList(shopCondition, 0100);
            modelMap.put("shopList", se.getShopList());
            // 列出店铺成功之后,将店铺放入session中作为权限验证依据,即该帐号只能操作它自己的店铺
            request.getSession().setAttribute("shopList", se.getShopList());
            modelMap.put("user", user);
            modelMap.put("success"true);
        catch (Exception e) {
            modelMap.put("success"false);
            modelMap.put("errMsg", e.getMessage());
        }
        return modelMap;
    }
 
    @RequestMapping(value = "/getshopbyid", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> getShopById(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Long shopId = HttpServletRequestUtil.getLong(request, "shopId");
        if (shopId > -1) {
            try {
                Shop shop = shopService.getByShopId(shopId);
                List<Area> areaList = areaService.getAreaList();
                modelMap.put("shop", shop);
                modelMap.put("areaList", areaList);
                modelMap.put("success"true);
            catch (Exception e) {
                modelMap.put("success"false);
                modelMap.put("errMsg", e.toString());
            }
        else {
            modelMap.put("success"false);
            modelMap.put("errMsg""empty shopId");
        }
        return modelMap;
    }
 
    @RequestMapping(value = "/getshopinitinfo", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> getShopInitInfo() {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        List<ShopCategory> shopCategoryList = new ArrayList<ShopCategory>();
        List<Area> areaList = new ArrayList<Area>();
        try {
            shopCategoryList = shopCategoryService.getShopCategoryList(new ShopCategory());
            areaList = areaService.getAreaList();
            modelMap.put("shopCategoryList", shopCategoryList);
            modelMap.put("areaList", areaList);
            modelMap.put("success"true);
        catch (Exception e) {
            modelMap.put("success"false);
            modelMap.put("errMsg", e.getMessage());
        }
        return modelMap;
    }
 
    @RequestMapping(value = "/registershop", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> registerShop(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        if (!CodeUtil.checkVerifyCode(request)) {
            modelMap.put("success"false);
            modelMap.put("errMsg""输入了错误的验证码");
            return modelMap;
        }
        // 1.接收并转化相应的参数,包括店铺信息以及图片信息
        String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
        ObjectMapper mapper = new ObjectMapper();
        Shop shop = null;
        try {
            shop = mapper.readValue(shopStr, Shop.class);
        catch (Exception e) {
            modelMap.put("success"false);
            modelMap.put("errMsg", e.getMessage());
            return modelMap;
        }
        CommonsMultipartFile shopImg = null;
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());
        if (commonsMultipartResolver.isMultipart(request)) {
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
            shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");
        else {
            modelMap.put("success"false);
            modelMap.put("errMsg""上传图片不能为空");
            return modelMap;
        }
        // 2.注册店铺
        if (shop != null && shopImg != null) {
            PersonInfo owner = (PersonInfo) request.getSession().getAttribute("user");
            shop.setOwner(owner);
            ShopExecution se;
            try {
                ImageHolder imageHolder = new ImageHolder(shopImg.getOriginalFilename(), shopImg.getInputStream());
                se = shopService.addShop(shop, imageHolder);
                if (se.getState() == ShopStateEnum.CHECK.getState()) {
                    modelMap.put("success"true);
                    // 该用户可以操作的店铺列表
                    @SuppressWarnings("unchecked")
                    List<Shop> shopList = (List<Shop>) request.getSession().getAttribute("shopList");
                    if (shopList == null || shopList.size() == 0) {
                        shopList = new ArrayList<Shop>();
                    }
                    shopList.add(se.getShop());
                    request.getSession().setAttribute("shopList", shopList);
                else {
                    modelMap.put("success"false);
                    modelMap.put("errMsg", se.getStateInfo());
                }
            catch (ShopOperationException e) {
                modelMap.put("success"false);
                modelMap.put("errMsg", e.getMessage());
            catch (IOException e) {
                modelMap.put("success"false);
                modelMap.put("errMsg", e.getMessage());
            }
            return modelMap;
        else {
            modelMap.put("success"false);
            modelMap.put("errMsg""请输入店铺信息");
            return modelMap;
        }
    }
 
    @RequestMapping(value = "/modifyshop", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> modifyShop(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        if (!CodeUtil.checkVerifyCode(request)) {
            modelMap.put("success"false);
            modelMap.put("errMsg""输入了错误的验证码");
            return modelMap;
        }
        // 1.接收并转化相应的参数,包括店铺信息以及图片信息
        String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
        ObjectMapper mapper = new ObjectMapper();
        Shop shop = null;
        try {
            shop = mapper.readValue(shopStr, Shop.class);
        catch (Exception e) {
            modelMap.put("success"false);
            modelMap.put("errMsg", e.getMessage());
            return modelMap;
        }
        CommonsMultipartFile shopImg = null;
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());
        if (commonsMultipartResolver.isMultipart(request)) {
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
            shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");
        }
        // 2.修改店铺信息
        if (shop != null && shop.getShopId() != null) {
            ShopExecution se;
            try {
                if (shopImg == null) {
                    se = shopService.modifyShop(shop, null);
                else {
                    ImageHolder imageHolder = new ImageHolder(shopImg.getOriginalFilename(), shopImg.getInputStream());
                    se = shopService.modifyShop(shop, imageHolder);
                }
                if (se.getState() == ShopStateEnum.SUCCESS.getState()) {
                    modelMap.put("success"true);
                else {
                    modelMap.put("success"false);
                    modelMap.put("errMsg", se.getStateInfo());
                }
            catch (ShopOperationException e) {
                modelMap.put("success"false);
                modelMap.put("errMsg", e.getMessage());
            catch (IOException e) {
                modelMap.put("success"false);
                modelMap.put("errMsg", e.getMessage());
            }
            return modelMap;
        else {
            modelMap.put("success"false);
            modelMap.put("errMsg""请输入店铺Id");
            return modelMap;
        }
    }
}

对比一下,复制你需要的部分,你看我格式化后的代码看得很清晰

0 回复 有任何疑惑可以回复我~
  • 提问者 Cris_hu #1
    可能用户回复没有代码格式功能,只有编辑里有
    回复 有任何疑惑可以回复我~ 2018-04-21 12:00:16
  • 翔仔 回复 提问者 Cris_hu #2
    直接粘贴到同学这边去 特别是对应的方法 记得清缓存 然后执行试试
    回复 有任何疑惑可以回复我~ 2018-04-21 12:14:23
翔仔 2018-04-21 11:42:39

controller呢。。。也贴上来,然后把替换成ajax的js代码也贴上来 你可以像我那样用那种方便阅读的代码方式提交代码呀。。。左上角有个代码语言 选择js或者java 粘贴出来 这样才方便阅读

0 回复 有任何疑惑可以回复我~
  • 提问者 Cris_hu #1
    好像这个回复不能选择代码。。。只有回复和取消两个按键
    回复 有任何疑惑可以回复我~ 2018-04-21 11:54:17
翔仔 2018-04-21 11:05:54

同学如果还没解决,记得把你前后端代码贴出来,然后你一定要用ajax来试试

0 回复 有任何疑惑可以回复我~
  • 提问者 Cris_hu #1
    我重新编辑了问题,前端代码在上面,这里是后台代码
    @Autowired
    	private ShopService shopService;
    	@Autowired 
    	private ShopCategoryService shopCategoryService;
    	@Autowired
    	private AreaService areaService;
    	@RequestMapping(value="/getshopbyid",method=RequestMethod.GET)
    	@ResponseBody
    	private Map<String,Object> getShopById(HttpServletRequest request){
    		Map<String,Object> modelMap=new HashMap<String,Object>();
    		Long shopId=HttpServletRequestUtil.getLong(request, "shopId");
    		if(shopId>-1){
    			try{
    				Shop shop=shopService.getByShopId(shopId);
    				List<Area> areaList=areaService.getAreaList();
    				modelMap.put("shop", shop);
    				modelMap.put("areaList", areaList);
    				modelMap.put("success", true); 
    			}catch(Exception e){
    				
    				modelMap.put("success",false);
    				modelMap.put("errMsg",e.getMessage());
    			}
    		}else{
    			modelMap.put("success",false);
    			modelMap.put("errMsg", "empty shopId");
    		}
    		return modelMap;
    	}
    回复 有任何疑惑可以回复我~ 2018-04-21 11:47:59
  • 翔仔 回复 提问者 Cris_hu #2
    同学,你的编辑栏左上角有个代码格式化的一个列表,有选java和js的 你在里面编辑啊,这样根本看不出来,而且你的controller提供错了吧,不是进入这个方法吧?我给你提供我的源码,你贴进去试试,这样可以吧?
    回复 有任何疑惑可以回复我~ 2018-04-21 11:56:39
  • 提问者 Cris_hu 回复 翔仔 #3
    老师,其实我最初的问题描述的挺详细的。。。就operationjs,我再调试url?userId=1的时候,前端页面没有显示后台返回的对应用户店铺信息,原因是getshopinfo里的getjson只把id信息发给了后台,之后的function代码都没有执行导致,我不知道为何没执行,后台modelmap正确
    回复 有任何疑惑可以回复我~ 2018-04-21 12:47:44
翔仔 2018-04-20 23:28:31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
                $.ajax({
                    url : productPostUrl,
                    type : 'POST',
                    data : formData,
                    contentType : false,
                    processData : false,
                    cache : false,
                    success : function(data) {
                        if (data.success) {
                            $.toast('提交成功!');
                            $('#captcha_img').click();
                        else {
                            $.toast('提交失败!');
                            $('#captcha_img').click();
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
   
                    alert(XMLHttpRequest.status);
                   alert(XMLHttpRequest.readyState);
                   alert(textStatus);
                  }
                });
            });

替换成这样的形式提交给后台,然后error里面应该就能获取到信息

0 回复 有任何疑惑可以回复我~
  • 提问者 Cris_hu #1
    还是不行。。。刚刚亲眼目睹了诡异的现象:在进入到了getShopInfo函数后,进入到15行执行.getJSON(shopInfoUrl,function(data)之后直接结束(并没有等待后台返回值)所以即使后台正确但是因为前端js函数体已经结束导致根本无法写入,也就是getShopInfo函数值完成了一个传入数据到后台的任务就结束了,老师您给的新的ajax代码没有用,console没有任何信息
    回复 有任何疑惑可以回复我~ 2018-04-21 00:16:01
  • 翔仔 回复 提问者 Cris_hu #2
    你要不把前后端相关代码贴出来我明天有空看下,早上有个春招面试明天要加班得早到早休息 ajax绝对有用 你说的肯定是正常现象 因为ajax不会等待方法返回的 但是断点打在回调函数上肯定是有返回的 但是那个getJson只有不报异常才返回 有异常就不会返回了 所以得一定要替换成那个ajax 替换后清空浏览器缓存试一下 ajax里面的url替换成你后台对应url 那post改成get
    回复 有任何疑惑可以回复我~ 2018-04-21 00:52:17
  • 提问者 Cris_hu 回复 翔仔 #3
    好的,我再试试
    回复 有任何疑惑可以回复我~ 2018-04-21 12:01:45
翔仔 2018-04-20 16:06:13

把页面缓存清空一下,然后再在chrome里的if(data.success){打个断点,不是在eclipse里,是在chrome的开发者模式里。。。 然后注意看看chrome控制台报什么错

0 回复 有任何疑惑可以回复我~
  • 提问者 Cris_hu #1
    我是按照老师你的步骤调的(之前也是在前端设置了调试,后台也是设置了调试),我又试了,还是不行,为了方便你理解我的情况我重新编辑我的问题,贴图片上去,如图我在14,15,16,17均设置了断点,14行进入后台,返回modelMap后,前端直接跳过了151617三个断点执行到了最后(也就是if判断没进去)
    回复 有任何疑惑可以回复我~ 2018-04-20 23:19:34
  • 提问者 Cris_hu #2
    要不我把我的代码发给你,然后看看老师您在你的环境下能不能正常显示
    回复 有任何疑惑可以回复我~ 2018-04-20 23:21:32
  • 翔仔 回复 提问者 Cris_hu #3
    同学这个是不行的哈,每个同学都发代码给翔仔的话肯定忙不过来的,一定要自己解决问题,目前来看基本能知道你的问题所在了,数据能过去,但是回不来,就说明你后台返回前端交互的地方出错了,往往是key值没对上导致的,你可以用ajax替代往后台提交的方法,在里面用error标记,然后打断点在error里,这样就能够定位出错误了
    回复 有任何疑惑可以回复我~ 2018-04-20 23:26:28
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号