java 带有 CSS 的 JSP 不显示背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6167990/
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-10-30 14:39:04  来源:igfitidea点击:

JSP with CSS does not display the background image

javahtmlcssjsptomcat

提问by Greg

I have the following directory structure

我有以下目录结构

structurehttp://img853.imageshack.us/img853/7092/96816871.jpg

结构体http://img853.imageshack.us/img853/7092/96816871.jpg

My CSS tries to use an image as a background

我的 CSS 尝试使用图像作为背景

#search-text {
width: 213px;
height: 28px;
padding: 6px 0 0 7px;
border: none;
background: url(../images/img02.jpg) no-repeat left top;
color: #000000;

}

}

and it doesn't work while other parts of css work fine.

它不起作用而 css 的其他部分工作正常。

Firebug shows that application tries to access image by URL _http://localhost:8080/images/img02.jpg and gets 404 error

Firebug 显示应用程序尝试通过 URL _http://localhost:8080/images/img02.jpg 访问图像并获得 404 错误

When I try to access image directly I also get this error. Also I tried _http://localhost:8080/paygate/images/img02.jpg _http://localhost:8080/paygate/resources/images/img02.jpg ...and this error doesn't stop follow me.

当我尝试直接访问图像时,我也会收到此错误。我也试过 _http://localhost:8080/paygate/images/img02.jpg _http://localhost:8080/paygate/resources/images/img02.jpg ...这个错误并没有停止跟随我。

When I've changed my CSS file to the following

当我将 CSS 文件更改为以下内容时

background: url(images/img02.jpg) no-repeat left top;

warning appeared in webserver log:

网络服务器日志中出现警告:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/paygate/images/img02.jpg] in DispatcherServlet with name 'appServlet'

and Firebug showed 404 error for URL _http://localhost:8080/paygate/images/img02.jpg

和 Firebug 显示 URL 404 错误 _http://localhost:8080/ paygate/images/img02.jpg

How should I organize my directory structure or what should I do to make my images accessible. Thank's!

我应该如何组织我的目录结构或者我应该怎么做才能使我的图像可访问。谢谢!

P.S. I'm using springsource tc server as a webserver.

PS 我使用 springsource tc 服务器作为网络服务器。

回答by Genzer

According to your project structure, the .css file and images folder is in the same level, So:

根据你的项目结构,.css文件和images文件夹在同一层,所以:

background: url(images/img02.jpg) no-repeat left top;

will work.

将工作。

回答by BalusC

Static resources are supposed to be served by the container's default servlet. However whenever you map any servlet on an URL pattern of /or /*in your webapp, then the default servlet will be completely overridden. You don't want to have that. Map the Spring dispatcher servlet on a more specific URL pattern, e.g. /app/*or *.htmlor whatever. If you don't want to have changes in the URL, then you need to put static resources in a fixed folder like /staticand create a Filterwhich will forward the non-static resources to the Spring dispatcher servlet.

静态资源应该由容器的默认 servlet 提供服务。但是,无论何时您将任何 servlet 映射到您的 web 应用程序的 URL 模式上//*在您的 web 应用程序中,默认 servlet 将被完全覆盖。你不想拥有那个。地图上的一个更具体的网址模式春季调度的servlet,如/app/**.html或什么的。如果您不想更改 URL,则需要将静态资源放在一个固定文件夹中,例如/static并创建一个Filter将非静态资源转发到 Spring 调度程序 servlet 的文件夹。

See also:

也可以看看: