java Spring Boot 不加载静态资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38770630/
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
Spring Boot not loading static resources
提问by SME
There are loads of questions about spring boot not loading static resources and having read them all (almost) I still can't fix this issue. At this stage I have opted not to run with spring boot but I'd still like to know what the issue was. I am using Eclipse, Java 8 and Maven.
有很多关于 Spring Boot 没有加载静态资源并且已经阅读所有(几乎)的问题我仍然无法解决这个问题。在这个阶段,我选择不使用 spring boot 运行,但我仍然想知道问题是什么。我正在使用 Eclipse、Java 8 和 Maven。
I have an application class that looks like this:
我有一个看起来像这样的应用程序类:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I have created a css file - src/main/resources/static/style.css
and referenced this from a jsp:
我创建了一个 css 文件 -src/main/resources/static/style.css
并从 jsp 中引用了它:
<link href="<c:url value='style.css'/>" rel="stylesheet">
The page loads but not the css. This is the error - 405 Method Not Allowed
页面加载但不是 css。这是错误 - 405 Method Not Allowed
I think think is correct but not sure. All help appreciated.
我认为认为是正确的,但不确定。所有帮助表示赞赏。
Based on some of the comments below this is how things look now.
根据下面的一些评论,这就是现在的样子。
My jsp files are configured in src/main/resources/application.properties as follows:
我的jsp文件在src/main/resources/application.properties中配置如下:
spring.mvc.view.prefix:/WEB-INF/views/
spring.mvc.view.suffix:.jsp
My Jsp is very simple, and is located in /WEB-INF/views/home.jsp
我的Jsp很简单,位于/WEB-INF/views/home.jsp
<!DOCTYPE html>
<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<link href="public/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<p>Hello world!</p>
</body>
</html>
I have also tried linking my css file like this:
我也试过像这样链接我的 css 文件:
<link href="style.css" rel="stylesheet" type="text/css"/>
My css file, located in webapp/public/style.css, is also very simple
我的css文件,位于webapp/public/style.css,也很简单
p {
color: red;
}
My jsp loads but not the css. I have run my application using various methods including:
我的jsp加载但不是css。我使用各种方法运行我的应用程序,包括:
From the command line - java -jar contacts.jar
从命令行 - java -jar contacts.jar
Inside eclipse - mvn spring-boot:run and mvn tomcat7:run-war Also inside eclipse by right clicking the Application.class file and selecting Run As -> Java Application.
在 eclipse 中 - mvn spring-boot:run 和 mvn tomcat7:run-war 同样在 eclipse 中通过右键单击 Application.class 文件并选择 Run As -> Java Application。
I am using Spring Boot Version 1.4.0.RELEASE
我正在使用 Spring Boot 版本 1.4.0.RELEASE
回答by Minjun Yu
CSS Location
CSS 位置
Put your static resources such as css outside of src/main/resources
, which is used for application resources such as properties files.
将 css 等静态资源放在 之外src/main/resources
,用于应用程序资源,例如属性文件。
I always put css files under src/main/webapp/assets/css
folder.
我总是把 css 文件放在src/main/webapp/assets/css
文件夹下。
Configuration
配置
I am using Java config instead of XML, the following snippet shows you how to configure spring boot to recognize and find the css.
我使用的是 Java 配置而不是 XML,以下代码段向您展示了如何配置 Spring Boot 以识别和查找 css。
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware{
// view resolver and other confugurations ommitted...
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
}
}
Access the css
访问 css
<link href="<c:url value="/assets/css/style.css" />" rel="stylesheet">
<link href="<c:url value="/assets/css/style.css" />" rel="stylesheet">
回答by Achille_vanhoutte
By default Spring Boot will serve static content from a directory called src/main/resources/static(or /publicor just /resourcesor /META-INF/resources) . When your project is created, Spring Boot will automatically provide the static folderunder src/main/resources. For that reason i store all my static resources inside the static folder.
In your case i would place one style.css file in each and every one of these folders and start a debugging process by removing them one by one until ... you know.
默认情况下,Spring Boot 将从名为 src/main/resources/ static(或 / public或仅 / resources或 / META-INF/resources)的目录中提供静态内容。创建项目时,Spring Boot 会自动提供src/main/resources 下的静态文件夹。出于这个原因,我将所有静态资源存储在静态文件夹中。
在你的情况下,我会在这些文件夹中的每一个中放置一个 style.css 文件,并通过将它们一一删除来开始调试过程,直到......你知道。
来源:
回答by saiboten
Try looking for a controller with a RequestMapping with a http method thats not GET, that is accepting your style.css request. This can be any request mapping without "value".
尝试寻找一个带有 RequestMapping 的控制器,它的 http 方法不是 GET,它接受你的 style.css 请求。这可以是没有“值”的任何请求映射。
This has happened to me when I moved to spring boot from jetty. In the old jetty solution the static content was served from a different servlet.
当我从码头搬到弹簧靴时,这发生在我身上。在旧的码头解决方案中,静态内容由不同的 servlet 提供。
回答by JesseBoyd
I struggled with connecting my static resources to my jsp pages and this is what I finally used to get it working with Spring boot 2.0. You can see my properties and also what the urls look like when mapping to static resources like images or plain html.
我努力将我的静态资源连接到我的 jsp 页面,这就是我最终用来让它与 Spring boot 2.0 一起工作的方法。您可以查看我的属性以及映射到图像或纯 html 等静态资源时的 url 外观。
Next we need to define the template prefix and suffix for our JSP files in application.properties. Thus add:
接下来,我们需要在application.properties 中为我们的 JSP 文件定义模板前缀和后缀。因此添加:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.servlet.context-path=/pdx
http://localhost:8080/pdx/images/thedocks.jpgaccess static resources in src/main/resources/static/images/thedocks.jpg
http://localhost:8080/pdx/images/thedocks.jpg访问 src/main/resources/static/images/thedocks.jpg 中的静态资源
http://localhost:8080/pdx/loads index.html in src/main/resources/static/index.html
http://localhost:8080/pdx/在src/main/resources/static/index.html 中加载index.html
http://localhost:8080/pdx/css/home.cssloads css class in src/main/resources/static/css/home.css
http://localhost:8080/pdx/css/home.css在src/main/resources/static/css/home.css 中加载 css 类
http://localhost:8080/pdx/hloads my home controller with @Controller("/") and @GetRequest(“/h”) annotations.
http://localhost:8080/pdx/h使用 @Controller("/") 和 @GetRequest(“/h”) 注释加载我的家庭控制器。
my jsp page loads the static image like this
我的jsp页面像这样加载静态图像
<img alt="the docks" src="/pdx/images/thedocks.jpg"/>
回答by Suraj Amble
Add in your application.properties file:
在 application.properties 文件中添加:
spring.mvc.static-path-pattern=/resources/**
And make sure your all static files are inside the src/main/resources/static/{css,js,other files}directory.
并确保所有静态文件都在 src/main/resources/ static/{css,js,other files}目录中。
To get CSS File use spring uri like this--->
要获取 CSS 文件,请像这样使用 spring uri--->
<spring:url var="css" value="/resources/css"/>
then link it like
然后链接它像
<link href="${css}/bootstrap.min.css" rel="stylesheet">
回答by Jovo Skorupan
Since Eclipse is mentioned in original question I am linking with this one: Java class getResource() with eclipse
由于在原始问题中提到了 Eclipse,我将其与这个链接起来:Java class getResource() with eclipse
Sometimes correct configuraton will not work when spring boot jar is run from eclipse. This can be resolved by adding "main\resources" folder to run configuration classpath manually ("run configurations"->"Classpath tab"->"User entries")
有时,当从 Eclipse 运行 spring boot jar 时,正确的配置将不起作用。这可以通过添加“main\resources”文件夹来手动运行配置类路径来解决(“运行配置”->“类路径选项卡”->“用户条目”)
回答by Aman Tuladhar
Assuming you are not disabling WebMvcAutoConfiguration
假设你没有禁用 WebMvcAutoConfiguration
Create the public
folder on the webapp
dir
创建public
的文件夹webapp
目录
And on you JSP
page
在你的JSP
页面上
<link href="public/style.css" rel="stylesheet" type="text/css"/>
Assuming you style.cssinside publicfolder i.e
webapp > public > styles.css
假设你在public文件夹中的style.cssie
webapp > public > styles.css
Here's a docs describing the how to use static content in much more detail.