1. 添加maven依赖:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
2. 工程总一定(创建)要有:dispatcher-servlet.xml。并在web.xml中引入这个配置文件:
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--1表示要启动的时候加载DispatcherServlet,这里默认加载固定命名的dispatcher-servlet.xml
当然,可以重命名,然后告诉制定的路径就可以了!-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!--配置请求过滤器,只接收*.do的请求-->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置默认启动的主页 START -->
<welcome-file-list>
<welcome-file>/html/TEST.html</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.xml:这里配置一个bean是采用基于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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自动扫描方式装载bean -->
<context:component-scan base-package="com.any.demoSpring.controller"/>
</beans>
3. 前端TEST.html:刚我们再web.xml中配置了默认启动主页是:/html/TEST.html,所以我们在这个页面使用jQuery发起ajax http请求:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TEST</title>
<style>
body{
text-align: center;
}
</style>
</head>
<body>
<div>
<input id="inVal" type="text" /><button id="btn">发送</button>
</div>
<script src="../js/lib/jquery-3.2.1.js"></script>
<script>
$('#btn').click(function () {
var inVal = $('#inVal').val();
$.ajax({
url: "postTest.do",
type: "POST",
dataType: "text",
contentType:"application/text;charset=UTF-8",
data:inVal,
success: function(data) {
alert(data);
},
error: function() {
alert("请求出错!");
}
});
});
</script>
</body>
</html>
4. 后台:刚我们在dispatcher-servlet.xml配置了一个扫描的controller包。所以我们只需在这个com.any.demoSpring.controller包下创建一个类,并编写响应方法即可:
package com.any.demoSpring.controller;
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;
@Controller
public class TestController {
@RequestMapping(value="postTest.do",method= RequestMethod.POST,produces = "application/text;charset=UTF-8;")
@ResponseBody
public String postTest(@RequestBody String str){
System.out.print("\n后台接收到前台的数据="+str+"\n");
return "来自后台的数据:"+str;
}
}
5.启动Tomcat操作!