spring-mvc 前端post表单数据到后台,后台没有接收到,并且用HttpServletRequest获取参数列表为空,下面为代码:
web.xml:
<servlet>
<servlet-name>embers</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/embers-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>embers</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
mvc配置文件:
<context:component-scan base-package="embers.blog.controller" />
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了,我漏了什么吗?
By the way, even if you use springmvc’s annotation: RequestParam, you can’t get it, and the entire form data is empty
I used a tool to write the message character by character. I also tried it with jsp and got the same 400
Error return:
Browser debugging, message sent:
In the background, use HttpServelet to get the ContentType, which is indeed application/x-www-form-urlencoded, but when you get the entire ParameterMap or a single Parameter, it is empty. When you get the body, it is also empty, but change it to GET and put the parameters in the url. No problem at all
I arrived at the company today and typed the code again. The company can get the exact same code, and all the tests are passable. I don’t know why I can’t get it at home. Let’s analyze the reason:
Initially estimated to be the tomcat version. The bug caused by the tomcat version used in the company is 8.0.29. I don’t know about it at home. I will go back tonight to check the tomcat version and check the bug list of this version
Continuous updates. . .
It’s fixed. It’s a bug in tomcat8.0.29. Please pay attention when using form data. Of course, don’t worry about writing rest
What about the form? Are you sure those fields are written in the form?