maven工程引用css和js文件

1.工程目录如下

 

maven工程引用css和js文件

目的:

在index.jsp中引用js文件

实现:

在web.xml中,新增

<servlet-mapping>

     <servlet-name >default </servlet-name >         

<url-pattern >*.js</url-pattern>      

</servlet-mapping >

<servlet-mapping >

     <servlet-name >default </servlet-name >             

<url-pattern >*.css</url-pattern>        

</servlet-mapping >

 

这两段代码,写在引入springMVC代码前。

 

jsp页面,写上

<%

      String path = request.getContextPath();

      String basePath = request.getScheme() + "://"

                  + request.getServerName() + ":" + request.getServerPort()

                  + path + "/";

%>

 

这段java代码,然后像这样使用

<script type= "text/javascript" src= "<%=basePath %>js/jquery-3.2.1.min.js"></script >

 

2.如果idea的jsp页面 request.getContextPath()报错

在项目中添加tomcat下的lib包

步骤:

1)File->Project Structure->Libraries->点击“+”->java;添加本机目录下的tomcat的lib包

 

maven工程引用css和js文件

2)pom.xml中添加依赖

<dependency>

<groupId>javax.servlet.jsp.jstl</groupId>

<artifactId>jstl-api</artifactId>

<version>1.2</version>

<exclusions>

<exclusion>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

</exclusion>

<exclusion>

<groupId>javax.servlet.jsp</groupId>

<artifactId>jsp-api</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.glassfish.web</groupId>

<artifactId>jstl-impl</artifactId>

<version>1.2</version>

<exclusions>

<exclusion>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

</exclusion>

<exclusion>

<groupId>javax.servlet.jsp</groupId>

<artifactId>jsp-api</artifactId>

</exclusion>

<exclusion>

<groupId>javax.servlet.jsp.jstl</groupId>

<artifactId>jstl-api</artifactId>

</exclusion>

</exclusions>

</dependency>