java - SpringMvc Post表单数据的一个问题

查看:162
本文介绍了java - SpringMvc Post表单数据的一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

解决方案

顺便提一下,即使是用springmvc的注解 : RequestParam ,也是取不到的,整个表单数据都是空的

我是用工具自己一个一个字符写的报文,也用jsp试过,同样拿到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>

错误返回:

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

浏览器调试,发送的报文:

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

在后台,用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的不用管

这篇关于java - SpringMvc Post表单数据的一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆