亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 642 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號