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