Static resources with Spring configuration-class

2019-08-30 02:25发布

My jsp loads some additional file like css and js files like almost every website does with something like this:

<script src="js/dhtmlx/dhtmlx.js"></script>

The problem now is, that Spring wants to dispatch the request and isn't finding a proper handler for it, which is right. I tried the following in my config-class:

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/js/**").addResourceLocations("/js/");
}

which is not working, spring still wants to dispatch the request:

org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [...URI/js/dhtmlx/dhtmlx.css] in DispatcherServlet with name 'keza'

How can I tell spring that this is not a request which the framework should dispatch with the dispatcherServlet?

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-30 02:49

1.Using mvc resources

<mvc:resources mapping="/resources/**" location="/public-resources/"/>

This enables serving any static content whose location can be specified as a Spring Resource

2. Using mvc:default-servlet-handler:

<mvc:default-servlet-handler />

This provides a way to serve out the static content from the root of the web application even though the Dispatcher Servlet is registered at /, the details of how Spring does this is available at the Spring documentation site here - in brief the responsibility is delegated to the containers default servlet.

MVC resources using spring annotations

@Configuration  
@EnableWebMvc  
public class WebAppConfig extends WebMvcConfigurerAdapter {  

        @Override  
        public void addResourceHandlers(ResourceHandlerRegistry registry) {  
                registry.addResourceHandler("/resources/**").addResourceLocations("/public-resources/");  
        }  
}
查看更多
混吃等死
3楼-- · 2019-08-30 02:57

make sure your js folder is available under src\main\webapp\resources

查看更多
登录 后发表回答