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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8582577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:49:21  来源:igfitidea点击:

Spring MVC - include static files / javascript , css

springmodel-view-controller

提问by Ilkar

I have created MVC application.

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

I want to include js or css file into jsp.

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

My static files ar under:

我的静态文件位于:

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

My code to include jquery is:

我包含 jquery 的代码是:

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

and i cant load the js file into view.

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

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'

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

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

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

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

my web.xml is:

我的 web.xml 是:

<?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>

采纳答案by Tomasz Nurkiewicz

Change your DispatcherServletmapping to e.g:

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

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

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

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

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

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

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

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

回答by Josef Procházka

add this to you confing and modify the location according to your need.

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

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

see this How to handle static content in Spring MVC?

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

回答by Mentor Reka

Why not use the simple jsp core ?

为什么不使用简单的 jsp 核心?

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    
<link rel="stylesheet" type="text/css" href="<c:url value='/resources/css/bootstrap.css'/>" />

回答by Jesse

add mvc:resources in your config file(*-servlet.xml), you can find it works

在你的配置文件(*-servlet.xml)中添加 mvc:resources ,你会发现它有效

回答by Master Mind

I just followed Mkyong Tutorialto place css, js, jquery & image files. Its working for me.

我只是按照Mkyong 教程来放置 css、js、jquery 和图像文件。它对我来说有效。

In servlet-context.xml

在 servlet-context.xml 中

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/assets/" />

In JSP , import tag library

在 JSP 中,导入标签库

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

and add like

并添加喜欢

<link rel="stylesheet" href="<c:url value='/resources/css/custom.css'/>">

回答by Survivor

Use spring JSTL tags to include external script files or style sheets. First you should include the the taglib in JSP as follows.

使用 spring JSTL 标签来包含外部脚本文件或样式表。首先,您应该按如下方式在 JSP 中包含 taglib。

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

Then you can include extenal script file using,

然后你可以使用包含外部脚本文件,

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

回答by bluechip

I agree with your answer. But in the style.css file declare url that relate to path of image.

我同意你的回答。但是在 style.css 文件中声明与图像路径相关的 url。

--style.css--

--style.css--

.cwt-object0
{
    display: block;
    left: 2.62%;
    margin-left: -1px;
    position: absolute;
    top: 43px;
    width: 64px;
    height: 64px;
    background-image: url('/resources/images/object0.png');
    background-position: 0 0;
    background-repeat: no-repeat;
    z-index: 0;
}

How to use tag <spring:url></spring:url>in style.css file to see in browser IE/Firefox

如何使用<spring:url></spring:url>style.css 文件中的标签在浏览器 IE/Firefox 中查看

--jsp file ---

--jsp 文件 ---

<link href="<spring:url value="/resources/style.css"/>" rel="stylesheet" type="text/css" media="screen">