application-mvc.xml配置javascript
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </list> </property> </bean> <!-- 定義跳轉的文件的先後綴 ,視圖模式配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 這裏的配置個人理解是自動給後面action的方法return的字符串加上前綴和後綴,變成一個 可用的url地址 --> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> <!--視圖解析器配置優先級 --> <property name="order" value="1" /> </bean>
$.ajax請求代碼java
$.ajax({ url : host + '/utilitiesCharge/queryUtilitiesList.htm', type:'POST', dataType:'json', // 返回數據格式,此配置無關緊要 data: {item: typeChange, companyId: companyId, tel: mobile, typeNum: typeCode, billDate: billDate, payAmount: payAmount}, success:function(data,textStatus,jqXHR){ var resp = data; // data爲JSON對象 }, error:function(xhr,textStatus){ console.log('錯誤') console.log(xhr) console.log(textStatus) }
SpringMVC接收代碼web
@RequestMapping("/queryUtilitiesList") @ResponseBody public BaseResponse queryUtilitiesList(DLUtilitiesQueryRequest request) { logger.info("request params:" + JSONObject.toJSONString(request)); }
其中,data爲對象,ajax
data: {item: typeChange, companyId: companyId, tel: mobile, typeNum: typeCode, billDate: billDate, payAmount: payAmount}
則SpringMVC,可直接用對象接收spring
DLUtilitiesQueryRequest request
$.ajax請求代碼json
$.ajax({ url : host + '/utilitiesCharge/queryUtilitiesList.htm', type:'POST', dataType:'json',// 返回數據格式,此配置無關緊要 contentType:'application/json', // 聲明請求參數格式爲JSON data: JSON.stringify({item: typeChange, companyId: companyId, tel: mobile, typeNum: typeCode, billDate: billDate, payAmount: payAmount}),//JSON字符串 success:function(data,textStatus,jqXHR){
差別部分:mvc
contentType:'application/json', // 聲明請求參數格式爲JSON data: JSON.stringify({item: typeChange, companyId: companyId, tel: mobile, typeNum: typeCode, billDate: billDate, payAmount: payAmount}),//JSON字符串
SpringMVC接收代碼app
@RequestMapping("/queryUtilitiesList") @ResponseBody public BaseResponse queryUtilitiesList(@RequestBody DLUtilitiesQueryRequest request) { logger.info("request params:" + JSONObject.toJSONString(request)); }
差別部分:jsp
@RequestBody DLUtilitiesQueryRequest request
各項代碼參考上面url