Java 如何在Spring应用程序中正确配置静态资源处理程序?
我在春天是个新手,我有以下问题 在一个教程中,我发现了以下配置类示例:Java 如何在Spring应用程序中正确配置静态资源处理程序?,java,spring,spring-mvc,jakarta-ee,resources,Java,Spring,Spring Mvc,Jakarta Ee,Resources,我在春天是个新手,我有以下问题 在一个教程中,我发现了以下配置类示例: @EnableWebMvc @Configuration @ComponentScan(basePackages = { "com.mycompany.myproject.web.controller" }) public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(R
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "com.mycompany.myproject.web.controller" })
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
.................
..............
}
在前面的代码片段中,addResourceHandlers()方法是如何工作的?查看官方文档,它似乎了解到它添加了处理程序来服务静态资源,例如web应用程序根目录下特定位置的图像、js和css文件、类路径等。
在前面的例子中,它用于添加与Twitter引导框架相关的静态资源(比如CSS文件和JavaScript文件)
问题是,我正在处理的项目不使用Java配置,而是使用XML配置,我很难理解如何在XML配置中执行与前面代码片段相同的操作
特别是我有一个servlet context.xml文件,其中包含MVC配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="it.hp.miur" />
</beans:beans>
我想我必须把这个配置放到这个文件中,但我不知道怎么做。你能帮我吗
Tnx