Java 如何在jsp页面中使用css
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21953340/
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
how to use css in jsp pages
提问by EmreAltun
I am trying to use css file from my jsp pages but page does not see the css codes.
我正在尝试使用我的 jsp 页面中的 css 文件,但页面没有看到 css 代码。
this is my .jsp file;
这是我的 .jsp 文件;
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Foundation | Welcome</title>
<link rel="stylesheet" href="/sources/css/foundation.css" />
<script src="/sources/js/vendor/modernizr.js"></script>
</head>
<body>
this is my configuratin file
这是我的配置文件
@Configuration
@ComponentScan("com.sprhib")
@EnableWebMvc
public class BaseTestConfig {
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/sources/**").addResourceLocations("/sources/");
}
}
回答by JB Nizet
The webapp is not deployed as the root webapp. So it has a "context path". The path to access the index.html file which is at the root of your webapp, for example, is in fact something like
Web 应用程序未部署为根 Web 应用程序。所以它有一个“上下文路径”。例如,访问位于 web 应用程序根目录的 index.html 文件的路径实际上类似于
http://localhost:8080/myfirstwebapp/index.html
This is what the location bar of your browser contains, and /myfirstwebapp
is the context path of your application.
这是您浏览器的位置栏所包含的内容,/myfirstwebapp
也是您的应用程序的上下文路径。
So, if you page contains href="/sources/css/foundation.css"
, the browser will try to load the css file from
因此,如果您的页面包含href="/sources/css/foundation.css"
,浏览器将尝试从
http://localhost:8080/sources/css/foundation.css
and not from
而不是来自
http://localhost:8080/myfirstwebapp/sources/css/foundation.css
You thus need to prepend the context path to allthe absolute URLs in your webapp. Using the JSTL:
因此,您需要在 web 应用程序中的所有绝对 URL 之前添加上下文路径。使用 JSTL:
href="<c:url value='/sources/css/foundation.css'/>"
Without the JSTL:
没有 JSTL:
href="${pageContext.request.contextPath}/sources/css/foundation.css"
回答by wonhee
Try to call those css directly through your web browser with full URL path. It depends on what's your application path defined in your Tomcat or web server.
尝试通过具有完整 URL 路径的 Web 浏览器直接调用这些 css。这取决于您在 Tomcat 或 Web 服务器中定义的应用程序路径。
If it's mapped like http://yourdomain.com/( root ), it should be http://yourdomain.com/sources/css/foundation.css, but if it's mapped like http://yourdomain.com/testor something, URL for CSS should be http://yourdomain.com/test/sources/css/fundations.css.
如果它像http://yourdomain.com/( root )一样映射,它应该是http://yourdomain.com/sources/css/foundation.css,但如果它像http://yourdomain.com/test或一些东西,CSS 的 URL 应该是http://yourdomain.com/test/sources/css/fundations.css。
You may use either relative pass for those, or introduce PATH variable so that link always created like http://yourdomain.com/PATH/sources/css/foundations.cssin the source code.
您可以为这些使用相对传递,或者引入 PATH 变量,以便在源代码中始终创建链接,如http://yourdomain.com/PATH/sources/css/foundations.css。
回答by Justin Paul Pa?o
Remove the /in /sources. This will work
删除/中/来源。这将工作