基于spring3.0的springmvc的ajax数据请求

本文介绍了一个使用SpringMVC框架实现AJAX交互的具体案例,包括所需的依赖库、控制器类编写方式、页面端代码实现及配置说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 需要如下两个jar包:
jackson-core-asl-1.7.2jar
jackson-mapper-asl-1.7.2jar

2.在控制器包里编写如下类

    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import com.zy.po.User;

    @Controller
    @RequestMapping("myajax.do")
    public class MyAjaxController {
    @RequestMapping(params="method=test1",method=RequestMethod.GET)
        public @ResponseBody List<User> test1(String uname) throws Exception{ 
            String uname2 = new String(uname.getBytes("UTF-8"),"UTF-8");
            System.out.println(uname2); 
            List<String> list = new ArrayList<String>();
            list.add("hello");
            list.add("world!");
            return list;   //这里可以返回字符串,list集合, 对象,springmvc都将会它转化为jason格式
        }
    }

3.页面端添加如下代码

<%@ 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>ajax数据传输测试</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">
        <script>
        function createAjaxObj(){
                var req;
                if(window.XMLHttpRequest){
                    req = new XMLHttpRequest();
                }else{
                    req = new ActiveXObject("Msxml2.XMLHTTP");//IE
                }
                return req;
            }

        function sendAjaxReq(){
                var req = createAjaxObj();
                req.open("get","myajax.do?method=test1&uname=1111");    req.setRequestHeader("accept","application/json"); 
                req.onreadystatechange  = function(){
                    eval("var result="+req.responseText);
                    document.getElementById("div1").innerHTML=result[1].uname;
                }
                req.send(null);
            }
        </script>
      </head>
      <body>
        <a href="javascript:void(0);" onclick="sendAjaxReq();">测试</a>
        <div id="div1"></div>
      </body>
    </html>

4.在springmvc-servlet.xml中添加如下配置

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="cacheSeconds" value="0" />
        <property name="messageConverters">
            <list>   
                <bean                                           class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                </bean>
            </list>
        </property>
    </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值