java - Spring(带 Jade)资源

标签 java spring-boot jade4j

问题

我的 spring-boot 应用程序最近将路由从 host/endpoint 更改为 host/middle/endpoint。自更改以来,我遇到了一个问题,即找不到与新 url 结构相关的资源。以前,我可以引用诸如 link(rel='stylesheet', href='css/style.css') 之类的 css 样式表之类的资源,但现在记录器显示错误,提示找不到资源位于 /middleman/css/style.css

根据我的研究,我发现我需要做的是使用资源处理程序注册表。我已经创建了一个(如下所示)但它似乎没有用。我认为问题在于,即使我现在拥有资源注册表,我也没有在注册表中引用资源。解决这个问题并让所有资源从同一个地方加载而不考虑端点的正确方法是什么?我很可能遗漏了一些明显的 SOP

注意:这都是我项目的简化表示,目的是在不提供不必要信息的情况下给出正在发生的事情的想法。

项目结构

src
  main
    java
      com.mystuff.cool
        configurations
          ResourceConfiguration.java
        controllers
          RoutingController.java
        application
          Application.java
  resources
    static
      css
        footer.css
        style.css
      images
        place1.png
        location1.png
        spot1.png
        favicon.ico
      javascripts
        layout.js
    templates
      home.jade

应用类

@ComponentScan(basePackages = {"my.packages"})
@EnableAutoConfiguration
@EnableSAMLSSO
@Configuration
public class Application
{
    public static void main(String[] args)
    {
        SpringApplication.run(new Object[]{ Application.class, ServiceConfig.class, ResourceConfiguration.class}, args);
    }
}

资源配置

@EnableWebMvc
@Configuration
public class ResourceConfiguration extends WebMvcConfigurerAdapter
{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(31556926);
        registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(31556926);
        registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
    {
        configurer.enable();
    }
}

Controller

@Controller
public class RoutingController
{    
    @RequestMapping("/house/home")
    public String home(Model model)
    {
        model.addAttribute("title", "Home is where the heart is");
        commonModelTribs(model);
        return "home";
    }
}

首页

doctype html
html
  title Place-spedia #{title}
  link(rel='icon', href='images/favicon.ico')
  link(rel='stylesheet', href='css/style.css')  
  script(src='javascripts/layout.js')
  link(rel='stylesheet', href='css/footer.css')
body
    div#footer-icons
        a(href='place1')
            img#place1(src="images/place1.png")
        a(href='location1')
            img#location1(src="images/location1.png")
        a(href='spot1')
            img#spot1(src='images/spot1.png')

最佳答案

如果您使用的是spring boot,则无需担心资源配置,因为您已经通过自动配置配置了资源目录。自动配置的默认行为是在 resources/static 中查找。

您的问题出在您的 href 值上,请尝试插入前导正斜杠:

link(rel='icon', href='/images/favicon.ico')
link(rel='stylesheet', href='/css/style.css')  
script(src='javascripts/layout.js')
link(rel='stylesheet', href='/css/footer.css')

Spring 将您的应用程序路由到一个新的相对路径,因此通过将前导 / 放在您的 href 属性中,您告诉路由器绝对在 static 目录,而不是相对于 middle 目录。

关于java - Spring(带 Jade)资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42983460/

相关文章:

java - Apache FOP 2.0 - 为什么没有找到 SVG 的 ImagePreloader?

java - xml文件的同步方法

java - 将具体类对象持久化到数据库时,将抽象类中的字段设置为强制字段

node.js - 遍历jade/pugjs 中的对象数组

java - 来自 Java 的甲骨文。对象参数的 CallableStatement 和 Null?

Java FILE 输入输出和文件输出

java - Spring Boot 无法识别 MongoDB RestController 类

java - 如何使用Spring Boot和查询注释创建不同的对象类型?

javascript - 在 Jade 中内联 Javascript,同时保持缩进