Ich habe eine einfache Anwendung ohne die Konfigurationsdatei web.xml und spring-servlet.xml erstellt. Ich konfiguriere nur Java-Code. Dies ist die Projektstruktur:Jetty + SpringMVC: Fehler beim Festlegen von configureDefaultServletHandling, weil der Standard-Servlet-Name nicht gefunden werden kann
Das ist meine Hauptklasse:
public class TripMapServer {
private static final int DEFAULT_PORT = 8080;
private static final String CONTEXT_PATH = "/";
private static final String MAPPING_URL = "/";
private static final String CONFIG_LOCATION = "com.biendltb.config";
private static final String DEFAULT_PROFILE = "dev";
public static void main(String[] args) throws Exception {
new TripMapServer().startJetty(getPortFromArgs(args));
}
private static int getPortFromArgs(String[] args) {
if (args.length > 0) {
try {
return Integer.valueOf(args[0]);
} catch (NumberFormatException ignore) {
}
}
return DEFAULT_PORT;
}
private void startJetty(int port) throws Exception {
Server server = new Server(port);
server.setHandler(getServletContextHandler(getContext()));
server.start();
server.join();
}
private static ServletContextHandler getServletContextHandler(WebApplicationContext context) throws IOException {
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setErrorHandler(null);
contextHandler.setContextPath(CONTEXT_PATH);
contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL);
contextHandler.addEventListener(new ContextLoaderListener(context));
contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());
return contextHandler;
}
private static WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(CONFIG_LOCATION);
context.getEnvironment().setDefaultProfiles(DEFAULT_PROFILE);
return context;
}
}
ich eine andere Klasse erstellt namens WebConfig.java die Config für die Frühjahr-Servlet zu setzen:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.biendltb.controller"})
public class WebConfig extends WebMvcConfigurerAdapter{
private static final Charset UTF8 = Charset.forName("UTF-8");
@Autowired
public Environment env;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(stringConverter());
}
private StringHttpMessageConverter stringConverter() {
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "plain", UTF8)));
return stringConverter;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/src/main/resources/").setCachePeriod(Integer.MAX_VALUE);
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(Integer.MAX_VALUE);
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(Integer.MAX_VALUE);
registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(Integer.MAX_VALUE);
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/src/main/resources/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
}
Fehler beim Hinzufügen der Funktion configureDefaultServletHandling.
[org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: Unable to locate the default servlet for serving static content. Please set the 'defaultServletName' property explicitly.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: Unable to locate the default servlet for serving static content. Please set the 'defaultServletName' property explicitly.
Es kann den Standardnamen des Servlets nicht finden. Normalerweise ist der Standardname des Servlets "default".
Ich denke, vielleicht sollte ich den Servlet-Namen festlegen, um dieses Problem zu lösen.
Haben Sie eine Idee, dieses Problem zu lösen?
Entschuldigung, dass das Codeformat nicht korrekt angezeigt wird. Ich freue mich über Ihre Unterstützung. Vielen Dank.
Eine solche NoClassDefFoundError geschieht in der Regel als Folge eines Stackoverflow, in der Regel zwischen Spring MVC und dem Container Standard-Servlet – bla