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>

 

转载于:https://www.cnblogs.com/alloevil/p/6074725.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值