2

The Problem

My spring-boot application recently changed routing from host/endpoint to host/middle/endpoint. Since the change, I am running into an issue where the resources are not being found relative to the new url structure. Before, I could reference resources like css stylesheets like link(rel='stylesheet', href='css/style.css'), but now the logger shows an error saying it can't find the resource at /middleman/css/style.css.

From my research, I have found that what I need to do is use a resource handler registry. I have created one (as shown below) but it doesn't seem to be working. I think the problem is that even though I now have the resource registry, I am not referencing resources in the registry. What is the proper way to solve this problem and have all resources point load from the same place regardless of the endpoint? I very well may be missing some obvious piece of SOP

Note: This is all a dumbed down representation of my project in order to give the idea of what is going on without giving unnecessary information.

Project Structure

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

Application Class

@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);
    }
}

Resource Configuration

@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";
    }
}

Home Page

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')
4

1 回答 1

1

如果您使用的是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目录中查找。

于 2017-03-23T23:32:06.800 回答