为了账号安全,请及时绑定邮箱和手机立即绑定

SpringMvc Post表单数据的一个问题

SpringMvc Post表单数据的一个问题

慕工程0101907 2019-03-12 13:15:05
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:@Controllerpublic 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.1Host: 127.0.0.1:8080Content-Type: application/x-www-form-urlencodedCache-Control: no-cacheusername=111wqfewrf&password=321&=一直都是好好的,这么简单的demo,现在突然不行了,好久没写springmvc了,我漏了什么吗?
查看完整描述

2 回答

?
浮云间

TA贡献1829条经验 获得超4个赞

顺便提一下,即使是用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的不用管


查看完整回答
反对 回复 2019-04-25
?
慕的地10843

TA贡献1785条经验 获得超8个赞

表单呢?确认那些字段写在form里了?


查看完整回答
反对 回复 2019-04-25
  • 2 回答
  • 0 关注
  • 682 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号