在Spring MVC 3.2.4中将资源映射到jsp

时间:2013-11-19 16:12:02

标签: java css spring jsp spring-mvc

我有关于将css,js,images映射到我的jsp的问题: 我的项目是这样的:

的src /主/ web应用/ WEB-INF / web.xml中

    <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
</servlet-mapping>

的src /主/ web应用/ WEB-INF / servlet的context.xml中

  <mvc:annotation-driven />
 <mvc:resources mapping="/jsp/**" location="/jsp/" />

的src /主/ web应用/ WEB-INF / JSP / CSS / style.css中

的src /主/ web应用/ WEB-INF / JSP / JS / jQuery的func.js

的src /主/ web应用/ WEB-INF / JSP /图像/ sample.jpg

的src /主/ web应用/ WEB-INF / JSP / index.jsp的

<head>
    <title>Shop Around</title>
    <meta http-equiv='Content-type' content='text/html; charset=utf-8'/>
    <link rel='stylesheet' href='jsp/css/style.css' type='text/css' media='all'/>
    <script src='jsp/js/jquery-1.4.1.min.js' type='text/javascript'></script>
    <script src='jsp/js/jquery.jcarousel.pack.js' type='text/javascript'></script>
    <script src='jsp/js/jquery-func.js' type='text/javascript'></script>
</head>

1 个答案:

答案 0 :(得分:1)

CSS-Javascript资源不能包含在WEB-INF下,因为WEB-INF下的文件无法从外部访问。因此,浏览器将无法提出获取它们的请求。将它们直接放在webapp下的文件夹中。

此外,您不应为他们创建servlet-mapping