java - SpringMvc Post表单数据的一个问题
大家讲道理
大家讲道理 2017-04-18 10:15:17
0
2
846

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了,我漏了什么吗?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
Peter_Zhu

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

<form action="${pageContext.request.contextPath }/demo.ember" method="post">
        <input type="text" name="username"/>
        <input type="text" name="password"/>
        <input type="submit" value="提交"/>
</form>

Error return:

HTTP Status 400 - Required String parameter 'username' is not present

Browser debugging, message sent:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:26
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=7872810B7BE203B784A906C3D49418D1
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/BugTest/demo.jsp
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36

username=wsa&password=esdx

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?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template