在Spring MVC 3中包括CSS和JS文件 [英] Including css and js file in spring mvc 3

查看:57
本文介绍了在Spring MVC 3中包括CSS和JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Google上搜索了数小时的服务器,但是我能弄清楚这一点.当我尝试访问WEB-INF内外的css/js文件时.但是Tomcat仅向我显示404错误.

I have searched on Google for serveral hours , but I can figure out this . When I try to access css/js file both inside and outside WEB-INF . But Tomcat just shows me 404 error .

dispatcher-servlet.xml

dispatcher-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文件中,我尝试了所有这些

In my index.jsp file , I try all of these

<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-INF时,它们会映射webapp/resources中的资源.要维护目录树,您应该使用

Your configuration files map resources in webapp/resources, while you put it into WEB-INF. To maintain your directory tree you should use

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

这篇关于在Spring MVC 3中包括CSS和JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆