一 DispatcherServlet会拦截静态资源(img、css、js)的问题
DispatcherServlet会拦截到所有的资源,导致一个问题就是静态资源(img、css、js)也会被拦截到,从而不能被使用。解决问题就是需要配置静态资源不进行拦截,在springmvc.xml配置文件添加如下配置
mvc:resources标签配置不过滤
- location元素表示webapp目录下的包下的所有文件
- mapping元素表示以/static开头的所有请求路径,如/static/a 或者/static/a/b
案例:
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery.min.js"></script>
<script>
//页面绑定,指定点击事件
$(function () {
$("#btn").click(function () {
$.ajax({
url:"user/testAjax",
contentType:"application/json;charset=UTF-8",
data:"{'username':'tom','password':'123','age':'18'}",
dataType:"json",
type:"post",
success:function (data) {
}
});
});
});
</script>
</head>
<body>
<button id="btn">按钮</button>
</body>
</html>
applicationContext.xml
<?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">
<!--开启注解spring注解扫描-->
<context:component-scan base-package="cn.itcast"></context:component-scan>
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--前端控制器,配置哪些资源不拦截-->
<mvc:resources location="/js/" mapping="/js/**" />
<!--开启springmvc支持注解扫描-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
二 使用@RequestBody获取请求体数据
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery.min.js"></script>
<script>
//页面绑定,指定点击事件
$(function () {
$("#btn").click(function () {
$.ajax({
url:"user/testAjax",
contentType:"application/json;charset=UTF-8",
data:"{'username':'tom','password':'123','age':'18'}",
dataType:"json",
type:"post",
success:function (data) {
}
});
});
});
</script>
</head>
<body>
<button id="btn">按钮</button>
</body>
</html>
UserController
package cn.itcast.controller;
import cn.itcast.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/testAjax")
public void testAjax(@RequestBody String body){
System.out.println("testAjax执行了...");
System.out.println(body);//输出{'username':'tom','password':'123','age':'18'}
}
}
三 使用@RequestBody注解把json的字符串转换成JavaBean的对象 以及 使用@ResponseBody注解把JavaBean对象转换成json字符串,直接响应
要完成json和JavaBean的相互转换,那就必须要保证json中的属性和javaBean中的属性要保持一致
1. json字符串和JavaBean对象互相转换的过程中,需要使用jackson的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>
index.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/5/1
Time: 1:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery.min.js"></script>
<script>
// 页面加载,绑定单击事件
$(function(){
$("#btn").click(function(){
// alert("hello btn");
// 发送ajax请求
$.ajax({
// 编写json格式,设置属性和值
url:"user/testAjax",
contentType:"application/json;charset=UTF-8",
data:'{"username":"hehe","password":"123","age":30}',
dataType:"json",
type:"post",
success:function(data){
// data服务器端响应的json的数据,进行解析
alert(data);
alert(data.username);
alert(data.password);
alert(data.age);
}
});
});
});
</script>
</head>
<body>
<button id="btn">发送ajax的请求</button>
</body>
</html>
UserController
package cn.itcast.controller;
import cn.itcast.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/testAjax")
public @ResponseBody User testAjax(@RequestBody User user){
System.out.println("testAjax执行了...");
//客户端发送的是ajax的请求,传的是json数据,后端已经把json数据封装到user对象中了
System.out.println(user);
//做出响应
//模拟查询数据库
user.setUsername("rose");
user.setPassword("456");
user.setAge(21);
return user;
}
}