1、項目目錄
2、index.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2020/2/27 Time: 19:26 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" type="text/javascript"></script> <script> $(function() { $("#btn").click(function() { alert(100); }); }); </script> </head> <body> <button id="btn">按鈕</button> </body> </html>
3、springmvc.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--配置注解掃描包--> <context:component-scan base-package="com.ly.mvc"></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/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/images/" mapping="/images/**"/> <mvc:annotation-driven></mvc:annotation-driven> </beans>
4、總結
在web.xml中配置了springmvc對任何請求都會進行攔截,因此頁面中引入jquery.min.js時也會被攔截,解決方法在springmvc.xml中增加過濾靜態資源文件的配置