【Ajax】ajax 访问 Servlet 和 访问 springMVC的Controller

分两种情况来写。

1. 首先是 html 页面 使用 ajax 访问 Servlet:

目录结构:

web.xml 不用修改,因为 Test_Servlet.java 中 配置了 @WebServlet("/test") 。

 index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
	<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
	<script type="text/javascript">
		function ajax1(){
			alert("3333");
			$.ajax({
				type : "post",
				url : "test", // 此处的路径 与 springmvc-servlet.xml 中保持一致
				success : function(result){
					alert("666");
					$(window).attr("location",result);
				}
			});
		}
	</script>
</head>
<body>
	<form action="">
		<input type="button" value="submit2" onclick="ajax1()">
	</form>
</body>
</html>

Test_Servlet.java

package test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;

@WebServlet("/test")
public class Test_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("1111");
		request.setAttribute("message", "doGet");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("2222");
		request.setAttribute("message", "doPost");
	}


}

会看到 输出:

 

 

 

2.  html 页面 使用 ajax 访问 SpringMVC 的Controller : 

先附上 目录结构:

涉及到  web.xml    springmvc-servlet.xml    index.html    TestController 四个文件

需要导入 springmvc 的一些jar包。这里提供完整下载:http://how2j.cn/k/springmvc/springmvc-springmvc/615.html

web.xml:  配置了 springmvc 解析

<?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" 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">
  <display-name>Servlet</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

	<servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

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: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.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


	<bean id="simpleUrlHandlerMapping"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/test.do">testController</prop>
			</props>
		</property>
	</bean>
	<bean id="testController" class="controller.TestController"></bean>

</beans>


 

index.html :  很简单的程序

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
	<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
	<script type="text/javascript">
		function ajax1(){
			alert("3333");
			$.ajax({
				type : "post",
				url : "test.do", // 此处的路径 与 springmvc-servlet.xml 中保持一致
				success : function(result){
					alert("666");
					$(window).attr("location",result);
				}
			});
		}
	</script>
</head>
<body>
	<form action="">
		<input type="button" value="submit2" onclick="ajax1()">
	</form>
</body>
</html>

TestController.java:

package controller;

import javax.servlet.http.HttpServletRequest;

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

@Controller
public class TestController {

	@RequestMapping("/test.do")
	@ResponseBody
	public String test(HttpServletRequest req){
		
		System.out.println("TEST_CONTROLLER...");
		
		String[] result = new String[2];
		result[0] = "result.html";
		result[1] = "访问 Controller 成功,返回信息 result.html 到 ajax,通过 ajax 转发到 result.html 页面";

		return "result.html";
	}
}

result.html:

<!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">
<title>Insert title here</title>
</head>
<body>
	ajax 访问 controller 成功,转发到 此处。
</body>
</html>

访问的原理:  

web.xml 首先被解析,发现有  springmvc 的 servlet,开始寻找  springmvc-xxx.xml 的配置文件,

springmvc-xxx.xml 的配置文件 配置了 bean  和 访问路径映射,可以找到 controller 的 java 文件,

Testcontroller.java 文件中 写了  @RequsetMapping(“xxx”)访问路径指向的方法,会执行该方法。 

控制台输出:

页面输出:

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值