zoukankan      html  css  js  c++  java
  • Spring MVC ajax:post/get 的具体实现

    Post 方式

    1、自动注入

      a. pom.xml ---- 配置Maven,添加必要的jar包

     1 <!--用于 String-JSONObject 转换 -->
     2 <dependency>
     3     <groupId>org.json</groupId>
     4     <artifactId>org.json</artifactId>
     5     <version>chargebee-1.0</version>
     6 </dependency>
     7 
     8 <!--用于 SpringMVC 注入 -->
     9 <dependency>
    10     <groupId>org.codehaus.jackson</groupId>
    11     <artifactId>jackson-mapper-asl</artifactId>
    12     <version>1.9.8</version>
    13     <type>jar</type>
    14     <scope>compile</scope>
    15 </dependency>

      b.application-servlet.xml 创建 AnnotationMethodHandlerAdapter

    1 <!-- 方式1 -->
    2 <mvc:annotation-driven />
    3 <!-- 方式2 -->
    4 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    5 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    6 
    7 <!-- 两种方式等价的,mvc:annotation-driven 会自动注册方式2中的连个bean -->

      c.前端jsp页面 ---- Jquery的ajax

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <script type="text/javascript">
     5 $(function() {
     6     function ajaxtest(){
     7     var seaText = $("#search").attr("value");
     8     var data={};
     9     data.serachdata = seaText;        
    10     var url = "${pageContext.request.contextPath}/search1"
    11     $.ajax({
    12             url : url,
    13             type : 'post',
    14             dataType : 'json',
    15             data:JSON.stringify(data),
    16             contentType : "application/json; charset=utf-8",
    17             success:function(result){
    18                     console.log(result);
    19                 }
    20             }
    21         );
    22     }
    23     $("#search").on("keyup", ajaxtest);
    24 }//end ready
    25 </head>
    26 </script>
    27 <body>
    28 <div>
    29        <input id="search" type="text" name="search">
    30        <input id="btn1"type="submit" value="Search">
    31 </div>
    32 </body>
    33 </html>

      d.Controller 代码

      SpringMVC 根据@RequestMapping将前端的ajax请求映射到search 的controllerAnnotationMethodHandlerAdapter会把request中的json对象,采用合适方式自动转换成Map对象  ★★

    1 @RequestMapping(value = "search",method = RequestMethod.POST)
    2     @ResponseBody public String search(@RequestBody HashMap<String, String> hasmap)throws Exception{
    3           List<String> resultList = testMapper.searchByKeyWords(hasmap.get("serachdata")+"%");
    4          return new org.json.JSONArray(resultList).toString();
    5 }

    2. 通过HttpServletRequest request来接收json

      这种方式是比较常见的这里,我这给出Controller的代码:

    1 @RequestMapping(value = "search",method = RequestMethod.GET)
    2 @ResponseBody public String search(HttpServletRequest request ,HttpServletResponse response)throws Exception{
    3       org.json.JSONObject jsonob = new JSONObject(request.getParameter("jsonobj"));
    4       List<String> resultList = testMapper.searchByKeyWords("a%");
    5       return new org.json.JSONArray(resultList).toString();
    6 }    

    Get 方式

      由于get是通过URI传递参数的,所以没有办法使用自动注入,只能通过request来接收参数,Controller与post方式的类似。

    第一篇博文啊!希望大家多多指点....

       

  • 相关阅读:
    2017"百度之星"程序设计大赛
    2018省赛赛第一次训练题解和ac代码
    2018天梯赛第一次训练题解和ac代码
    rsa Round #71 (Div. 2 only)
    AtCoder Grand Contest 021
    Hello 2018
    Educational Codeforces Round 36 (Rated for Div. 2)
    Codeforces Round #462 (Div. 2)
    Codeforces Round #467 (Div. 2)
    [Offer收割]编程练习赛48
  • 原文地址:https://www.cnblogs.com/felixpan/p/4394383.html
Copyright © 2011-2022 走看看