• springmvc ajax&json处理


    1 使用HttpServletResponse来处理--不需要配置解析器

    @Controller
    public class AjaxController {
    
        @RequestMapping("/ajax")
        public void ajax(String name,HttpServletResponse resp) throws IOException{
            if ("sgcc".equals(name)) {
                resp.getWriter().print("true");
                System.out.println("true");
            }else {
                resp.getWriter().print("false");
            }
        }
    }

    前台

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
        <script type="text/javascript" src="jquery-1.8.3-min.js"></script>
    
        
        </head>
    
    <body>
    <input type="button" value="发送" id="btn">
      用户名:<input type="text" id="textName"/>
        <script type="text/javascript">
            $(function(){
                $('#btn').click(function(){
                    $.ajax({url:'ajax.do',
                        type:'POST',
                        data:{
                            name:$('#textName').val()
                        },
                        dataType:'text',
                        success:function(data,textStatus){
                            alert(data);
                            console.log(textStatus)
                        },
                        error:function(textStatus){
                            console.log(textStatus)
                        }
                    })
                })
            })
        </script>
      
      
      </body>
    </html>

    2 springmvc处理json数据

    a 导入jar包

    jackson-annotations-2.7.0.jar
    jackson-core-2.7.7.jar
    jackson-databind-2.7.8.jar

    b 配置json转换器

    <!-- 用于将对象转换为JSON -->
        <bean id="stringConverter"
            class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <bean id="jsonConverter"
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        </bean>
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <ref bean="stringConverter"/>
                    <ref bean="jsonConverter"/>
                </list>
            </property>
        </bean>

    c controller代码

    @Controller
    public class JsonController {
        @RequestMapping("json")
        @ResponseBody
        public List<User> json(){
            List<User> list = new ArrayList<User>();
            list.add(new User(1,"zhangsan","男"));
            list.add(new User(2,"nico","female"));
            list.add(new User(1,"jackson","男"));
            return list;
        }

    前台代码(不对。。)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
        <script type="text/javascript" src="jquery-1.8.3-min.js"></script>
    
        
        </head>
    
    <body>
    <input type="button" value="获取数据" id="btn">
    <table width="80%" align="center">
    <tr>
        <td>编号</td>
        <td>姓名</td>
        <td>性别</td>
    </tr>
    <tbody id="content">
    </tbody>
    </table>
        <script type="text/javascript">
            $(function(){
                $('#btn').click(function(){
                    $.post("json.do",function(data){
                    var html="";
                        for(var i=0;i<data.length;i++){
                            html+="<tr><td>"+data[i].id+"</td><td>"+data[i].name+"</td><td>"+data[i].sex+"</td></tr>";
                        }
                        $("content").html(html);
                    });
                    
                });
            });
        </script>
      
      
      </body>
    </html>
  • 相关阅读:
    计算机网络-数据结构-MAC帧头-IP头-TCP头-UDP头
    (考研)java网络编程
    多态(重点:方法的多态性和对象的多态性)
    JZOJ1497 景点中心 题解
    JZOJ1227 Coprime 题解
    JZOJ3966 Sabotage 题解
    JZOJ3056 数字 题解
    JZOJ3054 祖孙询问 题解
    【Luogu P2282】【JZOJ 4906】【NOIP2016提高组复赛】组合数问题 题解
    JZOJ4316【NOIP2015模拟11.5】Isfind 题解
  • 原文地址:https://www.cnblogs.com/alloevil/p/6074725.html
Copyright © 2020-2023  润新知