Spring MVC资源没有映射

Akh*_*iar 1 java spring spring-mvc

以下是我的mvc-dispatcher.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/view/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<mvc:annotation-driven />
<mvc:resources location="/static/" mapping="/static/**" />
<context:component-scan base-package="in.codejava.personal.controllers" />
</beans>
Run Code Online (Sandbox Code Playgroud)

我哪里错了?所有静态/*url都是由我创建的404控制器映射而不是静态资源.

WEB.XML

<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">

<display-name>Personal Web Blogs</display-name>
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!--    <servlet-mapping> -->
<!--        <servlet-name>default</servlet-name> -->
<!--        <url-pattern>/static/*</url-pattern> -->
<!--    </servlet-mapping> -->
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)

如果我删除评论部分,它可以正常工作.

nic*_*dos 5

我要采取一个平底船,并假设你正在使用传统的目录结构为你的JS,CSS,图像资源,像这样:

  • src/main/webapp/[js|css|images]

在这种情况下,您mvc:resources应该看起来像:

<mvc:resources mapping="/static/**" location="/" />
Run Code Online (Sandbox Code Playgroud)

你应该在你的JSP中引用它们,如下所示: "${pageContext.request.contextPath}/static/js/foo.js"