今天在写代码的时候犯了一个基本的逻辑错误,细细想来还是基础不扎实。
需求:发送请求http://localhost:9555/mavenweb/customer/vueCustomerListPage.do时,会进入到页面index.html,并且显示数据库中的数据。
但是我只写了一个方法,然后后台可以打印出数据,但是前端怎么也接受不到数据。其实这里进入了一个逻辑的死循环,我发送该请求,然后得到了数据,但是返回的还是index该页面。所以前端就加载该页面,然后一加载该页面,页面就会发送请求接受数据,但是返回的还是该页面。。。于是一个死循环就诞生了。说白了,就是只有一个请求。请求和得到的响应永远都是该页面。
@RequestMapping("/customer/vueCustomerList")
public String vueCustomerList(Model model){
List<Customer> customerList = customerService.getCustomerList();
model.addAttribute("customerList", customerList);
System.out.println("list集合"+customerList);
return "index";
}
解决方法:发送一个请求,该请求只返回一个页面,然后一加载该页面,页面就会发送另一个请求到后台请求数据,后台返回json格式的数据,渲染页面。数据显示成功。
@RequestMapping("/customer/vueCustomerListPage")
public String vueCustomerPage(){
return "VueCustomerList";
}
@RequestMapping("/customer/vueCustomerList")
@ResponseBody
public List<Customer> vueCustomerList(Model model){
List<Customer> customerList = customerService.getCustomerList();
model.addAttribute("customerList", customerList);
System.out.println("list集合"+customerList);
return customerList;
// return customerList;
}