响应json数据之过滤静态资源
1.在webapp目录新建js目录并导入jquery.min.js如图所示:
2. 在response.jsp编写如下代码:
<%--
Created by IntelliJ IDEA.
User: Adair
Date: 2020/7/2 0002
Time: 10:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>返回响应</title>
<script src="js/jquery.min.js"></script>
<script>
// 页面加载,绑定单击事件
$(function() {
$("#btn").click(function () {
alert("hello btn");
});
});
</script>
</head>
<body>
<button id="btn">发送ajax的请求</button>
</body>
</html>
3.使用TomCat运行结果如图:
4. 通过浏览器访问http://localhost:8080/response.jsp结果如图所示:
5.点击“发送ajax的请求”按钮,点击不动,这时需要修改springmvc.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">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.txw"/>
<!-- 视图解析器对象 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--文件所在的目录-->
<property name="prefix" value="/WEB-INF/pages/"/>
<!--文件的后缀名-->
<property name="suffix" value=".jsp"/>
</bean>
<!--前端控制器,哪些静态资源不拦截-->
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<!-- 开启SpringMVC框架注解的支持 -->
<mvc:annotation-driven/>
</beans>
如图所示:
6.重新部署项目,并点击“发送ajax的请求”按钮会跳转到如图所示: