我想用 spring boot 开发 web 应用程序,我想在 jsp 文件中解决我的 javascript 和 css 资源。
我在 dispatcher-servlet.xml 中从 jsp 配置我对这个文件的访问,如下所示:
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
在我的 jsp 文件中,我可以使用下面的代码来访问它:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<img class="first-slide home-image" src="<spring:url value="/resources/images/back1.jpg"/>" alt="First slide">
我如何在 Spring Boot 中配置 mvc:resources 映射?
最佳答案
看看docs .
By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext.
您也可以使用与您发布的 xml 相同的 Java 配置。只需覆盖
addResourceHandlers
@Configuration
的方法扩展 WebMvcConfigurerAdapter
的类@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
一篇关于 Spring 资源的好博文 - here .更新:
WebMvcConfigurerAdapter
已被弃用。相反,我们可以 implements WebMvcConfigurer
.addResourceLocations
像这样的方法:.addResourceLocations("file:///D:/beijing_pic_sample/")
关于spring - 如何在spring boot中配置Spring mvc静态资源映射配置:<mvc:resources mapping. ../>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40939802/