Spring MVC - 包括静态文件/ javascript 、 css

2023-11-26

我已经创建了 MVC 应用程序。

我想将js或css文件包含到jsp中。

我的静态文件位于:



- webapp
        -js/jquery.js
        -WEB-INF|
                |
                 - jsp/*.jsp
  

我的包含 jquery 的代码是:

<script type="text/javascript" src="<c:url value="js/jquery.js" />"></script>

我无法将 js 文件加载到视图中。

我看到日志中包含以下信息:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/pool/js/jquery.js] in DispatcherServlet with name 'appServlet'

这意味着 MVC 尝试将 url 映射到 js 文件。

我认为我的配置有问题,但我不知道是什么。

我的 web.xml 是:

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

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<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/spring/appServlet/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>

  <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


将其添加到您的confing中并根据您的需要修改位置。

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

看到这个Spring MVC 中如何处理静态内容?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring MVC - 包括静态文件/ javascript 、 css 的相关文章

随机推荐