springmvc4.0配置ajax請求json格式資料

來源:互聯網
上載者:User

標籤:string類   hand   3.2   mvc   actor   mit   manager   顯示   函數   

1.匯入相關jar包:jackson-annotation-2.5.4.jar,jackson-core-2.5.4.jar,jackson-databind-2.5.4.jar.

2.spring-servlet.xml中相關配置:

//命名空間加入mvc:xmlns:mvc="http://www.springframework.org/schema/mvc"//xsi:schemaLocation中補充:http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd

<!-- springmvc4.0的配置方法-->

<!-- 啟動註解驅動的mvc功能--><mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/> <context:annotation-config/><bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="favorPathExtension" value="false"></property></bean><bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean><bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> </list> </property></bean><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="jsonConverter"/> <ref bean="stringConverter"/> </list> </property></bean>

3.編輯代碼

  單擊btn,觸發ajax,調用controller方法,返回json格式資料,並將其顯示在頁面

  3.1  jsp代碼錶單部分:

<div id="showData">${msg}</div><form id="paging">      page:<input type="text" name="page" id="page"/>      rows:<input type="text" name="rows" id="rows"/>      <input id="sub" type="button" value="submit"/>  </form>

  3.2  js代碼ajax部分:

$(‘#sub‘).click(function(){          alert("111");          $.ajax({              type:"POST",              url:"sysLogPaging.do",              data:{                  "page":$("#page").val(),                  "rows":$("#rows").val()              },              dataType:"json",              success:function(data){                  var dataajax=JSON.stringify(data);  //將json對象資料轉為string類型,以便在頁面輸出                $("#showData").html(dataajax).show();            },                        error:function(){                alert("出錯");            }          });     });

  3.3  java  controller被調用部分代碼:

@RequestMapping(value="/sysLogPaging", method=RequestMethod.POST)//分頁調用函數,傳回型別是JSONObject    @ResponseBody  //必須寫,標示傳送流資料    public JSONObject getSystemLog_paging(String page,String rows){        toJson syslogjson = new toJson(); //自訂的tojson類,用於拼接項目所需的特定格式的json資料        return syslogjson.toJson(sysloglist,page,rows);    }

總結:1.所需jar包必須匯入,關於配置,springmvc4.0和3.0有所差別,需要注意。

     2.如果傳送流資料(string,json等格式)controller中 @ResponseBody 不可省略。

     3.要將ajax擷取的json對象資料返回到頁面相應位置,需調JSON.stringify();函數將對象轉為string型再行show()。

springmvc4.0配置ajax請求json格式資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.