1
votes

I have a Spring Web app where the dispatcher servlet is only used for static files. There also is a Jersey servlet for API calls from JavaScript, mapped on another URL pattern, not too relevant to my problem.

At the moment my entire dispatcher configuration looks like this:

@Configuration
@EnableWebMvc
public class DispatcherConfig extends WebMvcConfigurerAdapter {

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

There is a main.html file right under www in my classpath. If a request comes in for /main.html, the file is served correctly. Great.

Now, I would like this same file to be returned for requests on /, /part and a bunch other paths. Basically, I want some kind of path aliasing here, or direct mapping from path to file. How can I achieve it?

1
It might be easier to use a @RequestMapping method with multiple value paths. - Sotirios Delimanolis
@SotiriosDelimanolis With a @Controller? I don't even have (or want) any controllers. - Konrad Garus
Can't you just use your servlet container's default servlet for serving static? - madhead

1 Answers

1
votes

You can use the default servlet of the container to serve the static resources:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

And the mapping from path to file can be defined with this:

@Configuration
@EnableWebMvc
public class DispatcherConfig extends WebMvcConfigurerAdapter {
...

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/pathToStatic").setViewName("/static.html");
    }
...

}

The above mapping will forward a request for /pathToStatic to the static view static.html.