发表于: 2018-04-24 23:54:05

3 660


今天完成的事情:

1.解决了昨天的遗留问题,在post传递表单数据时spring显示Content type 'application/x-www-form-urlencoded' not supported

https://stackoverflow.com/questions/16469384/submit-a-handsontable-to-a-spring-mvc3-controller-content-type-application-x

问题是,当我们使用application / x-www-form-urlencoded时,Spring并不理解它是一个RequestBody。所以,如果我们想要使用它,我们必须删除@RequestBody注解。

然后尝试以下操作:

@RequestMapping(value="/bulkImport", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)public String importUsers(BulkUserImportEntries entries) throws Exception {
    Iterator itr = entries.getData().iterator();
    while(itr.hasNext()) {
        Object obj = (Object)itr.next();
    }
    return "redirect:/app/{appId}/user/{id}";}

请注意,删除了注释@RequestBody并添加了consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE


2.完成了添加和修改的jsp。

明天计划的事情

继续完善task2,做总结

遇到的问题:

update时使用put方式提交表单,但是浏览器显示为get方式,而且参数显示在了地址内

解决:

浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求。配置springmvc配置文件springmvc-servlet.xml

<!-- 浏览器不支持put,delete等method,由该filter将/xxx?_method=delete转换为标准的http delete方法 -->  
<filter>  
    <filter-name>HiddenHttpMethodFilter</filter-name>  
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
</filter>  
  
<filter-mapping>  
    <filter-name>HiddenHttpMethodFilter</filter-name>  
    <servlet-name>springmvc</servlet-name>  
</filter-mapping> 


    其中springmvc是DispatcherServlet的名称

.页面请求

<form:form action="/xxx/xxx" method="put">  
</form:form>


    生成的页面代码会添加一个hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服务端将post请求改为put请求

<form id="userInfo" action="/xxx/xxx" method="post">  
    <input type="hidden" name="_method" value="put"/>  
</form> 


    另外也可以用ajax发送delete、put请求


使用delete的时候遇到问题,说Request method 'DELETE' not supported

无法解决……看错误日志似乎于jetty有关

收获:

@RequestBody:用于注解控制器方法参数,表示将请求的数据转为对象

@ResponseBody:用于注解控制器方法,表示方法返回值被作为response返回


返回列表 返回列表
评论

    分享到