Porównaj ceny domen i usług IT, sprzedawców z całego świata

Typ WebMvcConfigurerAdapter jest przestarzały


Właśnie przechodzę na wiosenną wersję mvc
5.0.1.RELEASE
, ale nagle w eclipse STS WebMvcConfigurerAdapter jest oznaczony jako przestarzały
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
// to serve static .html pages...
registry.addResourceHandler("/static/**").addResourceLocations("/resources/static/");
}
....
}

Jak mogę to usunąć!
Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Od wiosny 5 wystarczy zaimplementować interfejs
WebMvcConfigurer
:
public class MvcConfig implements WebMvcConfigurer {

Dzieje się tak, ponieważ Java 8 wprowadziła domyślne metody w interfejsach, które obejmują funkcjonalność klasy
WebMvcConfigurerAdapter
Spójrz tutaj:
https://docs.spring.io/spring/ ... .html
https://docs.spring.io/spring/ ... .html
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Pracowałem nad równoważną biblioteką dokumentacji Swaggera o nazwie
Springfox
i stwierdziłem, że w wersji Spring 5.0.8 (obecnie uruchomionej) interfejs
WebMvcConfigurer
został zaimplementowany przez
class WebMvcConfigurationSupport
które możemy bezpośrednio przedłużyć.
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;public class WebConfig extends WebMvcConfigurationSupport { }

I tak użyłem go do skonfigurowania mojego mechanizmu obsługi zasobów w następujący sposób -
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Użyj
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
Z Spring Boot 2.1.4.RELEASE (Spring Framework 5.1.6.RELEASE) zrób tak
package vn.bkit;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;// Deprecated.
import org.springframework.web.servlet.view.InternalResourceViewResolver;@Configuration
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer { @Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".html");
return resolver;
} @Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}}
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Wiosną każde zgłoszenie zostanie rozpatrzone

DispatcherServlet
... Aby uniknąć statycznego żądania pliku przez DispatcherServlet (front contoller), konfigurujemy

statyczna zawartość MVC
https://www.baeldung.com/sprin ... urces
.

Spring 3.1.

wprowadził ResourceHandlerRegistry, aby skonfigurować ResourceHttpRequestHandlers do obsługi statycznych zasobów ze ścieżki klas, WAR lub systemu plików. Możemy programowo skonfigurować ResourceHandlerRegistry w naszej klasie konfiguracji kontekstu WWW.

  • dodaliśmy szablon
    /js/**
    w ResourceHandler uwzględnijmy zasób
    foo.js
    znajdujący się w katalogu
    webapp/js/
  • Dodaliśmy template
    / resources/static/**
    w ResourceHandler, a także zawiera zasób
    foo.html
    znajdujący się w katalogu
    webapp/resources/
<pre class="lang-java prettyprint-override">
@Configuration
@EnableWebMvc
public class StaticResourceConfiguration implements WebMvcConfigurer { @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("WebMvcConfigurer - addResourceHandlers() function get loaded...");
registry.addResourceHandler("/resources/static/**")
.addResourceLocations("/resources/"); registry
.addResourceHandler("/js/**")
.addResourceLocations("/js/")
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new GzipResourceResolver())
.addResolver(new PathResourceResolver());
}
}


Konfiguracja XML

<pre class="lang-xml prettyprint-override">
<mvc:annotation-driven/>
<mvc:resources mapping="/staticFiles/path/**" location="/staticFilesFolder/js/"
cache-period="60"/>

Treść statyczna MVC Spring Boot
https://docs.spring.io/spring- ... ntent

jeśli plik znajduje się w folderze WAR

webapp/resources

.
<pre class="lang-java prettyprint-override">
spring.mvc.static-path-pattern=/resources/static/**

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się