我是春季启动架构的新手。它说要让索引页面找到像js这样的静态资源,我们需要将它保存在" src / main / resources / static"。
目录结构:
Html文件:src / main / webapp / WEB-INF / jsp /
js文件:src / main / resources / static / js /
这是我的索引页面:
<html ng-app="RollbackApp">
<head>
<title>My Rollback View</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.js"></script>
<script type="text/javascript" src = "js/app.js"></script>
</head>
<body>
<div ng-controller="rollbackController"><p>
<button ng-click="rollback()">RollBack</button>
</p></div>
</body>
</html>
目前,索引页面无法加载我的&#34; app.js&#34;
我的Mvc配置类如下:
package com.manoj;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
* Created by manojma on 10/13/2017.
*/
@Configuration
@EnableWebMvc
public class ApplicationWebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/static/");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".html");
return resolver;
}
}
我无法找到原因,为什么它无法找到我的js文件。
请帮帮我。!!
答案 0 :(得分:1)
问题是您的资源处理程序未正确配置。您已将/resources/static/
设置为资源位置。但是,考虑到src/main/resources
完全放在您的类路径上,您应该放弃/resources
部分。另外,您应该提到您正在查看类路径,因此您应该使用classpath:/static/
。
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/static/");
}
除此之外,您已定义资源处理程序以从/resources/**
开始转发请求。这意味着,如果您相对请求js/app.js
,它将无法工作,因为它不会触发资源处理程序。您需要使用resources/js/app.js
:
<script type="text/javascript" src="resources/js/app.js"></script>
但是,如果您的目标是静态地提供某些HTML页面,那么将HTML页面放入src/main/resources/static
文件夹也会容易得多。默认情况下,Spring引导已经index.html
提供the welcome page但can be customized。
答案 1 :(得分:0)
你必须小心覆盖任何默认值
我努力将我的静态资源连接到我的jsp页面,这是我最终用它来使用Spring boot 2.0。您可以在映射到静态资源(如图像或纯HTML)时查看我的属性以及URL的外观。
接下来,我们需要在 application.properties 中为我们的JSP文件定义模板前缀和后缀。因此添加:(上下文路径&#39; pdx&#39;是可选的,您可以选择一个名称来匹配您的应用程序)
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.servlet.context-path=/pdx
http://localhost:8080/pdx/images/thedocks.jpg访问 src / main / resources / static / images / thedocks.jpg中的静态资源
http://localhost:8080/pdx/在s rc / main / resources / static / index.html
中加载index.htmlhttp://localhost:8080/pdx/css/home.css在s rc / main / resources / static / css / home.css中加载css类
http://localhost:8080/pdx/h使用@Controller(&#34; /&#34;)和@GetRequest(“/ h”)注释加载我的家庭控制器。
我的jsp页面加载了像这样的静态图像
<img alt="the docks" src="/pdx/images/thedocks.jpg"/>