页面尝试使用Spring MVC加载CSS资源时发生错误500

时间:2020-09-23 19:04:26

标签: java spring maven spring-mvc thymeleaf

当页面尝试使用Spring MVC加载CSS资源时,我遇到错误500。当我尝试在浏览器中打开任何资源时,将显示以下异常:

类型异常报告

消息处理程序调度失败;嵌套异常为 java.lang.NoClassDefFoundError:无法初始化类 org.springframework.web.accept.PathExtensionContentNegotiationStrategy $ JafMediaTypeFactory

说明服务器遇到意外情况,该情况是 阻止它满足请求。

例外

org.springframework.web.util.NestedServletException:处理程序调度 失败嵌套异常是java.lang.NoClassDefFoundError:无法 初始化类 org.springframework.web.accept.PathExtensionContentNegotiationStrategy $ JafMediaTypeFactory org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:978) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) javax.servlet.http.HttpServlet.service(HttpServlet.java:634) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

根本原因

java.lang.NoClassDefFoundError:无法初始化类 org.springframework.web.accept.PathExtensionContentNegotiationStrategy $ JafMediaTypeFactory org.springframework.web.accept.PathExtensionContentNegotiationStrategy.getMediaTypeForResource(PathExtensionContentNegotiationStrategy.java:156) org.springframework.web.accept.ServletPathExtensionContentNegotiationStrategy.getMediaTypeForResource(ServletPathExtensionContentNegotiationStrategy.java:95) org.springframework.web.servlet.resource.ResourceHttpRequestHandler.getMediaType(ResourceHttpRequestHandler.java:527) org.springframework.web.servlet.resource.ResourceHttpRequestHandler.handleRequest(ResourceHttpRequestHandler.java:348) org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:51) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) javax.servlet.http.HttpServlet.service(HttpServlet.java:634) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

这是在我的页面中调用CSS的地方:

藻类工厂 通过file://->页面

pom.xml

4.0.0 com.algaworks brewer 1.0.0-SNAPSHOT 战争 UTF-8
UTF-8 1.8 3.2 4.3.0.RELEASE 3.1.0 3.0.0.RELEASE
5.2.4。最终 ->
4.3.1。最终

   <maven-jaxb2-plugin.version>0.14.0</maven-jaxb2-plugin.version>
     </properties>
 <build>      <plugins>
      <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>${maven-compiler-pluging.version}</version>
          <configuration>
              <source>${java.version}</source>
              <target>${java.version}</target>
          </configuration>            
      </plugin>       </plugins>   </build>
 <dependencyManagement>       <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-framework-bom</artifactId>
          <version>${spring-framework.version}</version>  
          <type>pom</type>
          <scope>import</scope>       
      </dependency>       </dependencies>   </dependencyManagement>
 <dependencies>       <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>  
      <scope>compile</scope>          </dependency>  
      <!-- Servlet API -->    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>${servlet.version}</version>
      <scope>provided</scope>     </dependency>
      <!-- Thymeleaf -->      <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf</artifactId>
      <version>${thymeleaf.version}</version>
      <scope>compile</scope>      </dependency>   
      <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring4</artifactId>
      <version>${thymeleaf.version}</version>
      <scope>compile</scope>      </dependency>
      <!-- Bean Validation / Hibernate Validator-->       <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>${hibernate-validator.version}</version>
      <scope>compile</scope>      </dependency>
      <dependency>
  <groupId>org.glassfish.jaxb</groupId>
  <artifactId>jaxb-runtime</artifactId>       <version>2.3.2</version>    </dependency>       <dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>1.0.0.GA</version>     </dependency>       </dependencies>      </project>

和webconfig.xml

package com.algaworks.brewer.config;

import org.springframework.beans.BeansException;进口 org.springframework.context.ApplicationContext;进口 org.springframework.context.ApplicationContextAware;进口 org.springframework.context.annotation.Bean;进口 org.springframework.context.annotation.ComponentScan;进口 org.springframework.context.annotation.Configuration;进口 org.springframework.web.servlet.ViewResolver;进口 org.springframework.web.servlet.config.annotation.EnableWebMvc;进口 org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 进口 org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 导入org.thymeleaf.TemplateEngine;进口 org.thymeleaf.spring4.SpringTemplateEngine;进口 org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver; 导入org.thymeleaf.spring4.view.ThymeleafViewResolver;进口 org.thymeleaf.templatemode.TemplateMode;进口 org.thymeleaf.templateresolver.ITemplateResolver;

导入com.algaworks.brewer.controller.CervejasController;

@Configuration //可配置的类 @ComponentScan(basePackageClasses = {CervejasController.class}) // indica ao Spring como encontrar作为类controladoras @EnableWebMvc //通过网络进行模拟Web MVC公共类 WebConfig扩展了WebMvcConfigurerAdapter实现 ApplicationContextAware {私有ApplicationContext applicationContext;私人ITemplateResolver templateResolver(){ SpringResourceTemplateResolver解析器=新 SpringResourceTemplateResolver(); resolver.setApplicationContext(applicationContext); resolver.setPrefix(“ classpath:/ templates /”); resolver.setSuffix(“。html”); resolver.setTemplateMode(TemplateMode.HTML);返回解析器; }
@Bean public ViewResolver viewResolver(){ThymeleafViewResolver 解析器=新的ThymeleafViewResolver(); resolver.setTemplateEngine(templateEngine()); resolver.setCharacterEncoding(“ UTF-8”);返回解析器; }
@Bean public TemplateEngine templateEngine(){ SpringTemplateEngine engine = new SpringTemplateEngine(); engine.setEnableSpringELCompiler(true); engine.setTemplateResolver(templateResolver());返回引擎; }

@Override public void setApplicationContext(ApplicationContext applicationContext)抛出BeansException {// TODO自动生成 方法存根this.applicationContext = applicationContext; } @Override public void addResourceHandlers(ResourceHandlerRegistry注册表){ Registry.addResourceHandler(“ / **”)。addResourceLocations(“ classpath:/ static /”); }

}

0 个答案:

没有答案