[英]Static resources with Spring configuration-class
My jsp loads some additional file like css and js files like almost every website does with something like this: 我的jsp会加载一些其他文件,例如css和js文件,就像几乎每个网站都这样:
<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. 现在的问题是,Spring希望分派请求,并且没有为它找到合适的处理程序,这是正确的。 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: 这不起作用,Spring仍想调度请求:
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? 我怎么能告诉spring这不是框架应该用dispatcherServlet调度的请求?
确保您的js文件夹在src \\ main \\ webapp \\ resources下可用
1.Using mvc resources 1.使用mvc资源
<mvc:resources mapping="/resources/**" location="/public-resources/"/>
This enables serving any static content whose location can be specified as a Spring Resource 这样就可以投放位置可以指定为Spring 资源的任何静态内容
2. Using mvc:default-servlet-handler: 2.使用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. 即使Dispatcher Servlet在/处注册,这也提供了一种从Web应用程序根目录提供静态内容的方法,有关如何执行此操作的详细信息,请参见此处的Spring文档站点-简而言之,将职责委托给容器默认servlet。
MVC resources using spring annotations 使用Spring注释的MVC资源
@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/public-resources/");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.