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

Ilk*_*kar 10 model-view-controller spring

我创建了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>
Run Code Online (Sandbox Code Playgroud)

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

我看到包含信息的日志:

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

是什么意思,MVC尝试将url映射到js文件.

我认为我的配置有一些东西,但我不知道是什么.

我的web.xml是:

<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Jos*_*zka 19

将此添加到您的配置中并根据您的需要修改位置.

  <mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>
Run Code Online (Sandbox Code Playgroud)

请参阅如何处理Spring MVC中的静态内容?


Tom*_*icz 6

DispatcherServlet映射更改为例如:

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

或其他一些不冲突的url-pattern喜欢*.htm/controllers/*.请记住,从现在开始,所有控制器都只能通过此模式使用.

现在它拦截了Web应用程序中的所有内容,包括.js文件,图像等.

同样的事情hibernateFilter- 在获取.js文件时你真的不需要打开Hibernate会话,不是吗?