为什么我不能在POST请求中获取数据?

时间:2017-06-06 08:26:31

标签: jquery spring-mvc jqgrid

js文件是:

function pageInit() {
    jQuery("#list2").jqGrid({
        url: '/json.json',
        mtype: 'post',
        datatype: "json",
        colNames: ['ID', 'account', 'password'],
        colModel: [{
            name: 'ID',
            index: 'id',
            width: 60
        }, {
            name: 'account',
            index: 'username',
            width: 110
        }, {
            name: 'password',
            index: 'name asc, username',
            width: 110
        }],
        rowNum: 10,
        rowList: [10, 20, 30],
        pager: '#pager2',
        sortname: 'id',
        sortorder: "desc",
        viewrecords: true,
        caption: "exp",
        jsonReader: {
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems: false,
            id: "0"
        },
        prmNames: {
            rows: 'pageSize',
            sort: null,
            order: null,
            search: null,
            npage: null,
            nd: null
        }
    });
    jQuery("#list2").jqGrid('navGrid', '#pager2', {
        edit: false,
        add: false,
        del: false
    });
}

和springmvc中的控制器:

@RequestMapping(value = "/json.json",method = RequestMethod.POST)
@ResponseBody
public void b(HttpServletRequest request, @RequestParam(value = "page",required = false) String page) throws Exception{
    System.out.println(request.getParameterMap().size());
    InputStream inputStream = request.getInputStream();
    String result = IOUtils.toString(inputStream, "UTF-8");
    System.out.println(result);
    System.out.println(page);
    System.out.println(request.getMethod());
    System.out.println(request.getContentType());
}

和println显示:

0
pageSize=10&page=1
null
POST

请求标头是:

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:18
Content-Type:application/x-www-form-urlencoded
Cookie:Idea-9d783f23=f3d01b09-52e1-4f55-a692-17f4f2a1bbb0
X-Requested-With:XMLHttpRequest

和表单数据是:

pageSize:10
page:1

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         metadata-complete="true" version="3.0">

  <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>

  </servlet>
  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

spring-dispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="controller"/>

    <mvc:annotation-driven/>

    <mvc:default-servlet-handler/>

    <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".html"/>
        <property name="templateMode" value="HTML5"/>
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver"/>
    </bean>

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine"/>
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonHttpMessageConverter"/>
            </list>
        </property>
    </bean>

    <bean id="jsonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    </bean>
</beans>

但如果我将方法改为“get”,它就可以了。为什么会这样?

1 个答案:

答案 0 :(得分:0)

好吧..我修好了。由于我以前的项目需要上传大量的图片,所以我在stackoverflow找到了这个答案。HttpRequest maximum allowable size in tomcat?弗里曼说。现在我删除了新代码并完成了