目录
导入json转javabean,javabean转json的maven坐标
导入jquery包,搭建环境
web-app目录下新建js,css,images文件夹,导入js包
配置请求响应页面,response.jsp
<%--
Created by IntelliJ IDEA.
User: Mocar
Date: 2019/9/12
Time: 17:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
<%--异步请求环境--%>
<script src="js/jquery.min.js"></script>
<script>
//页面加载,绑定单击事件
$(function () {
$("#btn").click(function () {
alert("hello btn")
})
});
</script>
</head>
<body>
<a href="user/testString">testString</a><br>
<a href="user/testVoid">testVoid</a><br>
<a href="user/testModelAndView">testModelAndView</a><br>
<a href="user/testForwardAndRedirect">testForwardAndRedirect</a><br>
<button id="btn">ajax异步请求</button><br>
</body>
</html>
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>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化加载bean到ioc容器-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--解决乱码-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
springmvc.xml配置开启springmvc注解驱动
配置前端控制器不拦截哪些静态资源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启组件扫描,视图解析器,返回的jsp文件配置-->
<context:component-scan base-package="com.itcatst"></context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean id="stringToDateConverter" class="com.itcatst.utils.StringToDateConverter"></bean>
</set>
</property>
</bean>
<!--前端控制器,哪些静态资源不拦截 location和mapping内容不要写反-->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<!--开启springmvc框架的支持,即springmvc注解驱动开启-->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"></mvc:annotation-driven>
</beans>
location和mapping写反报错:
警告: No mapping found for HTTP request with
URI [/springmvc_response_war/js/jquery.min.js] in DispatcherServlet with name 'dispatcherServlet'
测试:
发送ajax的请求,jsp文件编写
<%--
Created by IntelliJ IDEA.
User: Mocar
Date: 2019/9/12
Time: 17:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
<%--异步请求环境--%>
<script src="js/jquery.min.js"></script>
<script>
$(function () {
$("#btn").click(function () {
//alert("aaaa")
//发送ajax请求
$.ajax({
//ajax编写格式
url:"user/testAjax",//(默认: 当前页地址) 发送请求的地址。
type:"post",//(默认: “GET”) 请求方式 (“POST” 或 “GET”)
contentType:"application/json;charset=UTF-8",//(默认: “application/x-www-form-urlencoded”) 发送信息至服务器时内容编码类型。
data:'{"username":"张三","password":"aaa","age":12}',
datatype:"json",
success:function (data) {//请求成功后回调函数。两个参数
//data 服务器响应请求返回的json数据,进行解析
alert(data)
alert(data.username)
alert(data.password)
alert(data.age)
}
})
});
});
</script>
</head>
<body>
<a href="user/testString">testString</a><br>
<a href="user/testVoid">testVoid</a><br>
<a href="user/testModelAndView">testModelAndView</a><br>
<a href="user/testForwardAndRedirect">testForwardAndRedirect</a><br>
<button id="btn">ajax异步请求</button><br>
</body>
</html>
ajax书写可以参照:https://blog.csdn.net/qq_38636998/article/details/83411835
导入json转javabean,javabean转json的maven坐标
<!--javabean json需要的jar包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
controller层请求处理
@RequestMapping("/testAjax")
//接收ajax请求
public @ResponseBody User testAjax(@RequestBody User user){//json转javabean
System.out.println("testAjax....");
System.out.println(user);
user.setUsername("lisi");
user.setAge(20);
return user;//javabean转json
}