【问题标题】:Including css and js file in spring mvc 3 [duplicate]在spring mvc 3中包含css和js文件[重复]
【发布时间】:2016-01-31 07:07:46
【问题描述】:

我已经在 Google 上搜索了几个小时,但我可以弄清楚这一点。当我尝试访问 WEB-INF 内外的 css/js 文件时。但 Tomcat 只是显示 404 错误。

调度程序-servlet.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/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">

    <context:component-scan base-package="controller" />
    <mvc:annotation-driven />
    <bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                      value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
        <mvc:resources mapping="/resources/**"  location="/resources/" />
</beans>

在我的 index.jsp 文件中,我尝试了所有这些

<link rel="stylesheet" href="<c:url value="/resources/css/site.css"/>"/>
<link rel="stylesheet" href="<c:url value="${pageContext.request.contextPath}/resources/css/site.css"/>"/>
<link href="/resources/css/site.css" rel="stylesheet">

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
         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"
         metadata-complete="false">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>/</welcome-file>
    </welcome-file-list>

</web-app>

【问题讨论】:

  • 您的目录让我感到困惑:是否有一个文件夹:“....Web Pages/resources/css/”包含“site.css”?顺便说一句:&lt;link rel="stylesheet" href="&lt;c:url value="/resources/css/site.css"/&gt;"/&gt; 是对的
  • 是的。这是正确的。但我已经在我的 servlet 中配置了它。
  • 您可以尝试在 WebPages 下创建资源文件夹,而不是在 Web-INF 中。请参阅Map Resouces in spring
  • 我在网页和 web-inf 下都创建了。但只是 404 错误
  • 我在你的 web.xml 中没有看到这样的配置,但是 dispatcher-servlet.xml 中的 spring 配置:` `

标签: java css spring jsp spring-mvc


【解决方案1】:

您的配置文件映射 webapp/resources 中的资源,而您将其放入 WEB-INF。要维护您的目录树,您应该使用

<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

【讨论】:

  • 我试过了,但是不行。无论如何,tks这么多
猜你喜欢
  • 2014-09-07
  • 2014-12-04
  • 2011-01-09
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2012-01-24
相关资源
最近更新 更多