我正在使用角框架的春季启动应用程序。我已经结构化的项目如下:春季启动Web应用程序不提供静态内容
- 的src/main/java的:弹簧引导文件,控制器,持久性
- 的src/main /资源:application.properties中
- 的src/main /应用程序:“ web应用”的文件,有角的,CSS,模块
为了具有弹簧提供静态内容下/应用我已经通过重写addResourceHandlers方法扩展的类WebMvcConfigurerAdapter:
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "com.myapp.controller" })
public class WebMvcConfig extends WebMvcConfigurerAdapter {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "/" }; //src/main/webapp works by default
private static final String[] RESOURCE_LOCATIONS = { "classpath:/src/main/app/", "classpath:/src/main/app/" };
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
if (!registry.hasMappingForPattern("/**")) {
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS).setCachePeriod(0);
}
if (!registry.hasMappingForPattern("/app/**")) {
registry.addResourceHandler("/app/**").addResourceLocations(RESOURCE_LOCATIONS).setCachePeriod(0);
}
}}
当尝试使用 http://localhost:8080/app/app.html
如果我重新命名成/ webapp的我能够针对http://localhost:8080/app.html
/app文件夹,我得到一个HTTP错误404由于某些原因要访问角度主文件。我肯定错过了一些东西,但无法弄清楚它是什么。谢谢
您在应用程序下有'src/main/app'。你是否配置了Maven或Gradle来包含该目录?此外,为什么不遵循标准的Spring Boot约定来实现静态内容? – geoand 2014-09-22 10:33:19
我建议阅读spring引用参考指南(尤其是那些默认已经映射的引用)和使用webjars。基本上你的WebMvcConfig类中有什么是Spring Boot已经完成的,所以你只是复杂的事情。 – 2014-09-22 13:01:27
@geoand是的,我使用build-helper-maven-plugin将源目录的'src/main/app'添加到maven。使用标准的src/main/static和评论WebMvcConfig类我仍然有同样的问题。 – user3278795 2014-09-22 13:28:49