`

springmvc异步ajax请求

 
阅读更多

spring使用了jackson类库,帮助我们在java对象和jsonxml数据之间的互相转换。他可以将控制器返回的对象直接转换成json数据,供客户端使用。客户端也可以传送json数据到服务器进行直接转换。

第一步,导入spring jar(commons-logging-1.0.4.jarjackson-core-asl-1.8.7.jarjackson-mapper-asl-1.8.7.jar)

 

第二步,项目配置文件web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>GBK</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

 

第三步,springMVC配置文件springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	    http://www.springframework.org/schema/context 
	    http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	>

	<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<context:component-scan base-package="com.test.controller" />
	
	<mvc:annotation-driven/>

	<!--对模型视图名称的解析,即在模型视图名称添加前后缀 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
		<property name="prefix" value="/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

 

步,实体类Employee.java

package com.test.model;

public class Employee {
	private int id;
	private String name;

	public Employee(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 

步,控制器AjaxController.java

package com.test.controller;

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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.model.Employee;

@Controller
public class AjaxController {

	//@ResponseBody此注解用于处理ajax请求,返回值可以是任何值,spring会自动转换为json对象   
	@RequestMapping(value = "/ajax", method = RequestMethod.GET)
	public @ResponseBody List<Employee> testAjax() throws Exception {
		
		List<Employee> list = new ArrayList<Employee>();
		list.add(new Employee(1, "赵四"));
		list.add(new Employee(2, "王五"));

		return list;//注意返回此时值可以不再是一般的String
	}
}

 

前台页面/ajax.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>测试ajax</title>
<script type="text/javascript">
	function createAjaxObject(){
		var request;
		if(window.XMLHttpRequest){
			request = new XMLHttpRequest();
		}else{
			request = new ActiveXObject("Msxml2.XMLHTTP");//ie
		}
		return request;
	}
	
	function sendAjaxRequest(){
		var request = createAjaxObject();
		request.open("get", "ajax");
		//必须加上此头,spring才知道发出的是ajax请求
		request.setRequestHeader("accept", "application/json");
		request.onreadystatechange = function(){
			eval("var result = "+request.responseText);//eval函数 
			document.getElementById("div").innerHTML = result[0].id + "	" + result[0].name;
		};
		request.send(null);
	}
</script>
</head>
<body>
	<a href="javascript:void(0)" onclick="sendAjaxRequest()">测试Ajax</a>
	<div id="div"></div>
</body>
</html>

 

浏览器输入:

http://127.0.0.1:9900/springmvc_008_ajax/ajax.jsp

页面显示:

 

  • 大小: 13.8 KB
  • 大小: 814 Bytes
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics