Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

Servlet 返回的html静态资源寻找失败

  • 在springboot项目中重写了一个servlet并且配置
server:
  port: 8132
 servlet:
    context-path: /aaa
  • servlet 配置
@Bean
public ServletRegistrationBean commonServlet() {
   if (log.isDebugEnabled()) {
      log.debug("开始初始化common servlet");
   }
   ServletRegistrationBean servletServletRegistrationBean = new ServletRegistrationBean();
   servletServletRegistrationBean.setServlet(new ResourceServlet("/support/"));
   Map<String, String> initParams = new HashMap<>(10);
   HfViewConfig bean = context.getBean(HfViewConfig.class);
   if (bean != null && !bean.equals(new HfViewConfig())) {
      initParams.put(PARAM_NAME_USERNAME, bean.getLogin());
      initParams.put(PARAM_NAME_PASSWORD, bean.getPassword());
   }
   servletServletRegistrationBean.addUrlMappings(new String[]{"/common/*"});
   servletServletRegistrationBean.setInitParameters(initParams);
   if (log.isDebugEnabled()) {

      log.debug("开始初始化common servlet 完成.");
   }
   return servletServletRegistrationBean;
}
  • 在首页添加了 本地的静态资源
<link href="css/demo.css" rel="stylesheet">
<script src="js/a.js"></script>
  • 在页面上加载失败了如何解决

image.png

image.png

在druid中有下面图中文件
image.png
采用的读取方式即: 读取html文件本身,其html在浏览器中
image.png
它的url会被带有
image.png

相关druid配置代码

@Bean
public ServletRegistrationBean statViewServlet() {
   ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/drc/*");
   Map<String, String> initParams = new HashMap<>();
   initParams.put("loginUsername", "admin");   //设置登陆账号密码
 initParams.put("loginPassword", "123456");
   initParams.put("allow", "");
   bean.setInitParameters(initParams);
   return bean;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Spring Boot 加载静态资源的默认路径为

/META-INF/resources/  
/resources/  
/static/  
/public/ 

参见:
https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
https://spring.io/guides/gs/serving-web-content/

support并不在默认路径之列,所以下面的样式和脚本不会被加载到。试试放到上面的路径之一。

即,可以在resources/目录下创建一个static/public/目录,并将静态内容放在该目录下。通过以下方式可以访问它们http://localhost:8080/your-file.ext。(假设server.port是8080)

也可以通过在application.properties配置文件中使用spring.resources.static-locations中自定义静态资源目录。

例如:

spring.resources.static-locations=classpath:/custom/ 

现在,您可以使用custom/下方的文件夹resources/提供静态文件。

更新:

也可以使用java config:

@Configuration
public class StaticConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/custom/");
    }
} 

此配置将目录的内容映射customhttp://localhost:8080/static/**url。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...