我正在使用Spring Boot,并试图使我的静态资源(CSS,JS,字体)在部署时可用。源代码可供您查看或从https://github.com/joecracko/StaticResourceError.克隆
现在我的CSS、JS和字体文件对我部署的网站不可见。
下面是我的项目目录结构:
下面是编译后的JAR的根目录:我向您保证,这些文件存在于各自的文件夹中。
以下是我看到的网络错误:
以下是chrome tools提供的资料来源。请注意吧。css在这里显示为空。你可以看看我的源代码,看看它不是空的。
这是我的主页。html
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
<!-- Main Styles -->
<link rel="stylesheet" href="/css/bar.css" />
<script src="/js/foo.js"></script>
</head>
<body>
<div>Welcome to Foo!</div>
</body>
</html>
这是我的Web应用初始化器(FooWebAppInitializer.java)
public class FooWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(ServletConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
//Spring Security
container.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain"))
.addMappingForUrlPatterns(null, false, "/*");
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcherServlet", new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
dispatcher.addMapping("*.css");
dispatcher.addMapping("*.eot");
dispatcher.addMapping("*.svg");
dispatcher.addMapping("*.ttf");
dispatcher.addMapping("*.woff");
dispatcher.addMapping("*.map");
dispatcher.addMapping("*.js");
dispatcher.addMapping("*.ico");
}
}
下面是我的Servlet配置(ServletConfig.java)
@Configuration
@EnableWebMvc
@ComponentScan({"com.foo"})
public class ServletConfig extends WebMvcAutoConfiguration{
@Bean
MultipartResolver multipartResolver() {
return new StandardServletMultipartResolver();
}
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("messages");
return source;
}
}
还有我的Spring Security配置(WebSecurityConfig.java)
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().permitAll();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**"); // #3
}
}
有2件事要考虑(Spring Bug V1.5.2..Relax)- 1)检查所有的控制器类,用于@ EnabeWebMVC注释,如果有任何2,请删除它)检查使用注释的控制器类--RestController或@ Controller。不要在一个类中混合使用Rest API和MVC行为。对于MVC,使用@Controller;对于REST API,使用@RestController
做以上两件事解决了我的问题。现在我的spring boot正在加载静态资源,没有任何问题@控制器=
@Controller
public class WelcomeController {
// inject via application.properties
@Value("${welcome.message:Hello}")
private String message = "Hello World";
@RequestMapping("/")
public String home(Map<String, Object> model) {
model.put("message", this.message);
return "index";
}
}
指数html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet/less" th:href="@{/webapp/assets/theme.siberia.less}"/>
<!-- The app's logic -->
<script type="text/javascript" data-main="/webapp/app" th:src="@{/webapp/libs/require.js}"></script>
<script type="text/javascript">
require.config({
paths: { text:"/webapp/libs/text" }
});
</script>
<!-- Development only -->
<script type="text/javascript" th:src="@{/webapp/libs/less.min.js}"></script>
</head>
<body>
</body>
</html>
将静态资源放在目录下:
/src/main/resources/static
您还可以使用public
或resources
而不是static
作为文件夹名。
说明:您的构建工具(Maven或Gradle)将复制应用程序类路径中/src/main/资源/
中的所有内容,并且,正如在Spring Boot的文档中所写的那样,类路径中名为/静态
(或/public
或/资源
)的目录中的所有内容将作为静态内容。
此目录也可以使用,但不建议使用:
/src/main/webapp/
如果您的应用程序将打包为jar,请不要使用src/main/webapp目录。虽然这个目录是一个常见的标准,但它只适用于war打包,如果您生成一个jar,它将被大多数构建工具默默忽略。
我无法访问静态内容(angular app),甚至无法访问简单的索引。来自spring boot的html文件。我一直收到404错误。Spring没有为我提供这些静态文件。自从升级到Spring Boot 2.2.4后,我就遇到了这个问题。我必须升级以应对Zip64问题。 我的application.properties里有这样一句话: 我也有自己的staticResourceConfigurat
我有一个小型java webapp,由三个微服务组成——api-service、book-service和db-service,所有这些都使用mini kube本地部署在kubernetes集群上。 我计划为api-service和book-service保留单独的UI,从单独的pod提供常见的静态文件,可能是映像。 我能够创建一个前端,为参考本教程的nginx:alpine中的静态文件提供服务。
问题内容: 我无法让Spring-boot项目提供静态内容。 我已经放在一个命名的文件夹下。在其中,我有一个名为的文件夹。当我打包应用程序并运行它时,它找不到我放在该文件夹中的图像。 我试图把静态文件中,并但没有任何工程。 如果我我可以看到文件在正确文件夹的jar中:例如,但是调用:http://localhost:8080/images/head.png,我得到的只是一个404 有什么想法为什么
我有Spring Boot Web应用程序,最初是为内部Tomcat服务器构建的(有效)。然后我采用了该应用程序在Web Logic服务器上运行。我的应用程序编译并部署到服务器没有问题,但当它不服务MVC页面时。每次调用都会抛出404错误。从下面的错误看,它看起来像Spring调度程序servlet存在,但甚至区域设置都没有正确设置。我无法弄清楚这里有什么问题或缺失,但当我创建RestContro
问题内容: 我在两个不同的容器(Tomcat和Jetty)上部署了一个webapp,但是它们用于提供静态内容的默认servlet具有处理我要使用的URL结构的不同方式(详细信息)。 因此,我希望在web应用程序中包含一个小型servlet,以提供其自己的静态内容(图像,CSS等)。Servlet应该具有以下属性: No external dependencies Simple and reliab
谷歌云文档对该应用的可用语法不是很精确。用于我的节点的yaml文件。js应用程序。 我使用了GAE的Python留档中描述的语法,从中我发现了处理程序mecism: 我避免使用expressjs规则来服务/public/index。html内容,最后我得到了一个404错误,这意味着GAE不是作为静态内容提供我的页面: 你有什么线索吗?节点。js与制作API、生成动态内容相关。。。但我更喜欢使用谷歌