spring-mvc 前端post表单数据到后台,后台没有接收到,并且用HttpServletRequest获取参数列表为空,下面为代码:
web.xml:
embers
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:conf/embers-mvc.xml
1
true
embers
/rest/*
mvc配置文件:
controller:
@Controller
public class BugTestController {
@RequestMapping(value="/demo",method=RequestMethod.POST)
public String demo(HttpServletRequest request,HttpServletResponse response) throws IOException{
System.out.println(request.getParameterMap());
System.out.println(request.getParameter("username"));
System.out.println(request.getParameter("password"));
System.out.println(request.getContentType());
System.out.println("//");
response.setContentType("text/html");
response.getWriter().println("keke");
response.getWriter().flush();
response.getWriter().close();
return null;
}
}
我已经调试过无数次了,如果设定为GET方法,然后把参数放在URL上是完全没有问题的,用json提交,配置json转换也是没有问题的,就是默认的POST表单数据有问题,下面是调试工具生成的表单完整报文:
POST /BugTest/rest/demo HTTP/1.1
Host: 127.0.0.1:8080
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
username=111wqfewrf&password=321&=
一直都是好好的,这么简单的demo,现在突然不行了,好久没写springmvc了,我漏了什么吗?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
顺便提一下,即使是用springmvc的注解 : RequestParam ,也是取不到的,整个表单数据都是空的
我是用工具自己一个一个字符写的报文,也用jsp试过,同样拿到400
错误返回:
浏览器调试,发送的报文:
在后台,用HttpServelet获取ContentType,的确为application/x-www-form-urlencoded,但是获取整个ParameterMap或者单个Parameter,都是空,获取body,也是空,但是换成GET,参数放在url中,完全没有问题
今天到了公司,将代码重新敲了一遍,一模一样的代码,公司是可以取到的,各种测试都是通的,不知道为什么在家里取不到,分析下原因:
初步估计为tomcat版本引发的bug,公司中用的tomcat版本为8.0.29 ,家里的不知道,今晚回去查下tomcat版本,顺便查下该版本的bug list
持续更新。。。
fix了,是tomcat8.0.29的一个bug,大家用表单数据的时候注意了,当然,写rest的不用管
表单呢?确认那些字段写在form里了?