Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring应用程序中正确配置静态资源处理程序?_Java_Spring_Spring Mvc_Jakarta Ee_Resources - Fatal编程技术网

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