【springmvc (五)】使用ajax

本文详细介绍了在 Spring MVC 框架中如何利用 Ajax 进行与服务器的交互,包括环境搭建、配置文件设置及示例代码解析。

ajax是js中与服务器进行交互的手段,是一种异步请求方式,使用面还是很广的。下面介绍在springmvc中如何使用ajax。

1.环境搭建:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    id="WebApp_ID"   
    version="3.0">  
    
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/config/spring.xml</param-value>  
    </context-param>  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <!-- 配置spring核心servlet -->  
    <servlet>  
        <servlet-name>spring</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>spring</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping> 
</web-app>  
启动spring容器,并且指定了spring的配置文件和spring mvc的配置文件位置。都在web-inf下的config目录下。


spring。xml:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
    xmlns:p="http://www.springframework.org/schema/p">
    <!-- 定义一个bean -->
</beans>  
因为这里不需要在容器定义任意bean,所已空。


spring-mvc。xml:

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
   
    <mvc:annotation-driven />
    <!-- 自动扫描的包名 -->  
    <context:component-scan base-package="com.t"/>  
    <!-- 视图解释类 -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/jsp/"/>  
        <property name="suffix" value=".jsp"/>  
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
    </bean>  
    <mvc:resources location="/WEB-INF/js/" mapping="/resource/js/**" />
</beans>
这里要注意
 <mvc:annotation-driven />
必须写,会启动一些spring的类来处理json数据。

还有就是静态资源要配置,否则会被拦截。

扫描包的位置自行设定。

index。jsp文件:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="/resource/js/jquery.min.js"></script>
<title>Insert title here</title>
</head>
<body>


<h3 ><p>success jquery</p></h3>


<script type="text/javascript">
$(document).ready(function(){
<span style="white-space:pre">	</span>alert("json");
<span style="white-space:pre">	</span>$.ajax( {  
        url : "/json",  
        dataType:"json",  
        data:{"id":"ididid"},
        success : function(msg) {  
            console.log(msg);
            for(item in msg){
            <span style="white-space:pre">	</span>  alert(msg[item]);
            }
        }  
    });
});
</script>
</body>
</html>
controller:

package com.t;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AjaxController {

	@RequestMapping(value="/index.htm")
	public String index(){
		return "index";
	}
	@RequestMapping(value="/json")
	public @ResponseBody List<Integer> json(String id){
		System.out.println(id);
		List<Integer> list = new ArrayList<Integer>();
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		list.add(5);
		return list;
	}
}
接受一个ajax参数同时返回一个list。


需要引入的jar:

spring的一堆包,commons-logging包,jackson的包,jstl的包。

最后来个项目目录截图:


项目源码:

http://pan.baidu.com/s/1kUkxpiB

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值