springmvc中静态资源无法访问问题

来源:互联网 发布:网络态势感知 dfi 编辑:程序博客网 时间:2024/05/22 00:52

项目目录结构如下:


首先尝试了将静态资源文件放在了WEB-INF下,然后在spring配置文件中使用<mvc:resource>进行了配置

<?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.springmvc.*"></context:component-scan>    <mvc:annotation-driven></mvc:annotation-driven>    <!--配置数据源-->    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>        <property name="url" value="jdbc:mysql://localhost:3306/db_test"></property>        <property name="username" value="root"></property>        <property name="password" value="root"></property>    </bean>    <!-- ViewResolver -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>        <property name="prefix" value="/WEB-INF/views/"/>        <property name="suffix" value=".jsp"/>    </bean><!--静态资源配置-->    <mvc:resources mapping="/js/**" location="/WEB-INF/static/js"/></beans>
web.xml如下

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"         version="3.0">    <servlet>        <servlet-name>springmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <!--配置要加载的XML文件-->            <param-name>contextConfigLocation</param-name>            <param-value>classpath:springmvc-servlet.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>
index.jsp
<html><head>    <%        String path=request.getContextPath();        String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;    %>    <script type="text/javascript">    </script></head><body>    <a href="goHome">go Home</a></body></html>
IndexController

@Controllerpublic class IndexController {    @RequestMapping("/goHome")    public String goHome(){        return "home";    }}

通过index.jsp跳转到了home.jsp,在home.jsp中引用到了WEB-INF/static/js中的js文件,发现<mvc:resources mapping="/js/**" location="/WEB-INF/static/js"/>配置并没有起作用,静态资源文件依旧不能访问

home.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Home</title>    <%        String path=request.getContextPath();        String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;    %>    <script type="text/javascript" src="/static/js/index.js"></script></head><body>    home jsp</body></html>


试了网上说的三种办法:

(1)当前使用的方法,在spring配置文件中配置<mvc:resources mapping="/js/**" location="/WEB-INF/static/js"/>,但是并没有起作用

(2)使用<mvc:default-servlet-handler/>依旧不好用

(3)在web.xml中使用tomcat的defaultServlet来处理,但是发现静态资源还是不能放在WEB-INF下,只能放在之外才好用

https://my.oschina.net/hnqingping1255/blog/415575

第(3)种方法:

将资源文件移到了webapp下


在web.xml中配置tomcat的defaulthandler

 <servlet>        <servlet-name>default</servlet-name>        <servlet-class>            org.apache.catalina.servlets.DefaultServlet        </servlet-class>        <init-param>            <param-name>debug</param-name>            <param-value>0</param-value>        </init-param>        <init-param>            <param-name>listings</param-name>            <param-value>true</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.js</url-pattern>    </servlet-mapping>    
注意:需要添加catalina jar包

        <!--tomcat catlina-->        <dependency>            <groupId>org.apache.tomcat</groupId>            <artifactId>catalina</artifactId>            <version>6.0.26</version>            <scope>provided</scope>        </dependency>
测试,终于可以访问了:



目前,还没有找到使用第(1)种方法无效的原因,有空继续探索吧...

原创粉丝点击