前后端交互常见问题(SpringBoot)2020 您所在的位置:网站首页 php前后端数据交互 前后端交互常见问题(SpringBoot)2020

前后端交互常见问题(SpringBoot)2020

2023-07-28 02:09| 来源: 网络整理| 查看: 265

前后端交互常见问题(SpringBoot) thymeleaf实现时间格式转换、下拉框回显:SpringBoot前端返回的时间数据无法被识别后端无法获取前台传值从一个Controller方法跳到另外一个Controller方法后端编写的弹窗效果无法触发前端后端传值区分大小写!(血的教训,特别写在url上的,要好好查看清楚)idea连接数据库时区问题 前言:这是我在学习springboot时所遇到的一些大大小小问题,希望能通过记录学习过程中遇到的问题来见证自己的成长XD,也希望能帮到你。

thymeleaf实现时间格式转换、下拉框回显:

时间格式转换

1 全部 SpringBoot前端返回的时间数据无法被识别

error:

Field error in object 'bill' on field 'billTime': rejected value [2017-06-25]; codes[typeMismatch.bill.billTime,typeMismatch.billTime,typeMismatch.java.util. Date,typeMismatch];arguments[org.springframework.context.support.DefaultMessag eSourceResolvable: codes [bill.billTime,billTime]; arguments []; default message [billTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'billTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.baomidou.mybatisplus.annotation.TableField java.util.Date] for value '2017-06-25'; nested exception is java.lang.IllegalArgumentException]]

当时在后台接收的是一个对象,无法将时间单独拿出来处理,所以我采取了在前端修改时间格式的方式:

后端无法获取前台传值

问题:前端采用get方法,url为 http://localhost/getBills?index=1&date1=null&date2=null&typeid=null 需要特别注意的是,此处的null为字符串类型,极具欺骗性(对于新手…),与后端所需参数类型不一致,因此报错。

从一个Controller方法跳到另外一个Controller方法

使用重定向:

return "redirect:/请求"; 后端编写的弹窗效果无法触发 @RequestMapping("/update") public String update( Bill bill, HttpServletResponse response,ModelMap map){ int status = billService.update(bill); response.setContentType("text/html;charset=utf-8"); try { PrintWriter writer=response.getWriter(); if(status>0){ writer.print("alert('修改成功');"); } writer.print("alert('修改失败');"); } catch (IOException e) { e.printStackTrace(); } return "redirect:/list"; }

问题:可能是返回页面与请求页面不一致导致的。(不确定)

前端后端传值区分大小写!(血的教训,特别写在url上的,要好好查看清楚)

error:

java.lang.IllegalStateException: Optional int parameter 'typeId' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type. at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.handleNullValue(AbstractNamedValueMethodArgumentResolver.java:245) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:116) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]

原因: 前端请求url是http://localhost/getBills?index=1&date1=2017-06-28&date2=2017-08-25&typeid=0,后端接收的参数是typeId!,区分大小写!太粗心了,耗费了几个小时才找到原因。

idea连接数据库时区问题

解决: 时区为Hongkong即可。 在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有