Spring <mvc :resources mapping ="/css/**" location ="/css/"/> doesn't work

标签 spring spring-mvc

嗨,映射 CSS 不起作用。

这是我的配置:

spring-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
    
    <context:component-scan base-package="spring.controllers" />
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
       <property name="prefix" value="/WEB-INF/jsp/content/" />
       <property name="suffix" value=".jsp" />
    </bean>
    
    <context:component-scan base-package="it.jsoftware.jacciseweb.controllers"></context:component-scan>
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/js/**" location="/js/" />  
    

web.xml:

<?xml version="1.0" encoding="UTF-8"?>

  <display-name>JewishProjectT</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

和我的 heder 文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <c:choose>
            <c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18en'}">
                <c:set var="approach" value="left" scope="page"></c:set>
            </c:when>
            <c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18ru'}">
                <c:set var="approach" value="left" scope="page"></c:set>
            </c:when>
            <c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18he'}">
                <c:set var="approach" value="right" scope="page"></c:set>
            </c:when>
            <c:otherwise>
                <c:set var="approach" value="left"></c:set>
            </c:otherwise>
        </c:choose>
        <link rel="stylesheet" media="all" type="text/css" href="/css/common.css">
        <link rel="stylesheet" media="all" type="text/css" href="/css/lavamenu.css">
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script type="text/javascript" src="/js/<c:out value="${approach}"/>/lavalamp.js"></script>
        <title>${title}</title>
    </head>
    <body>
        <div id="main">

在 Chrome 中显示:

/js/left/lavalamp.js 404 (Not Found) 0:19 GET

/css/common.css 404 (Not Found) 0:17 GET

/css/lavamenu.css 404 (Not Found)

如何正确配置css/images/js的路径?

附注我排除了所有以 http:\\开头的链接,因为 stackoverflow 的规则不允许我发布它们,因为我是初学者。

最佳答案

您的<mvc:resources mapping="/css/**" location="/css/" />对我来说似乎很好,但是您确定您的应用程序在根目录中运行吗?您的网址不应该以其他上下文开头,例如:

href="/myapp/css/common.css"

或者,为了避免硬编码引用:

href="<c:url value="/css/common.css" />"

如果您的应用程序在根目录中运行,那么您可能需要正确的命名空间声明?像这样的东西:

<?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/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">

关于 Spring <mvc :resources mapping ="/css/**" location ="/css/"/> doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12226027/

相关文章:

java - 如何在 spring boot 中拥有线程安全 Controller

java - Hibernate JSR303 验证和错误生成的 propertyPath

java - 如何在虚拟机退出后创建临时文件并删除

java - 要下载哪个 Jetty 发行版才能运行完整的 Spring3.0 应用程序?

java - Spring Boot中未创建MongoDB唯一索引

java - Spring-modules-validation 导致错误

java - 通过 JMeter 代理 POST 请求结果是 HTTP 415 错误

spring - 返回 void 的 Controller 方法可以生成 JSON 吗?

spring - @Async 和 @Cacheable 缓存非空响应

自定义注释上的 Spring 切入点 XML 表达式