Spring MVC-包含静态文件/javascript,css [英] Spring MVC - include static files / javascript , css

查看:75
本文介绍了Spring MVC-包含静态文件/javascript,css的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created MVC application.

我想在jsp中包含js或css文件.

I want to include js or css file into jsp.

我的静态文件位于以下位置:

My static files ar under:


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

我包含jquery的代码是:

My code to include jquery is:

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

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

and i cant load the js file into view.

我看到带有信息的日志:

I see the logs with info:

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文件.

what means, that MVC try to map url to js file.

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

I think that there is something with my configuration, but i don't know what.

我的web.xml是:

my web.xml is:

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

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

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>

推荐答案

将您的DispatcherServlet映射更改为例如:

Change your DispatcherServlet mapping to e.g:

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

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

Or some other not conflicting url-pattern like *.htm or /controllers/*. Remember that from now on all your controllers will be available only through this pattern.

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

Now it is intercepting everything in your web application, including .js files, images, etc.

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

The same thing with hibernateFilter - you don't really need an open Hibernate session when fetching .js files, don't you?

这篇关于Spring MVC-包含静态文件/javascript,css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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