Недавно я начал изучать Spring MVC и по-прежнему не может запустить веб-приложение, выдает ошибку в браузере. Запускаю через Tomcat.
Вот мои файлы:
Код: Выделить всё
package ru.alliance.springMVC.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MySpringMVCServlet extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class[] getRootConfigClasses() {
return null;
}
@Override
protected Class[] getServletConfigClasses() {
return new Class[] {SpringConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
Код: Выделить всё
package ru.alliance.springMVC.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
@Configuration
@ComponentScan("ru.alliance.springMVC")
@EnableWebMvc
public class SpringConfig implements WebMvcConfigurer{
private final ApplicationContext applicationContext;
@Autowired
public SpringConfig(ApplicationContext applicationContext) {
this.applicationContext=applicationContext;
}
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
registry.viewResolver(resolver);
}
}
Код: Выделить всё
package ru.alliance.springMVC.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class FirstController {
@GetMapping("/hello")
public String helloPage() {
return "first/hello";
}
@GetMapping("/goodBye")
public String goodByePage() {
return "first/goodBye";
}
}
Код: Выделить всё
GoodBye
GoodBye!
Код: Выделить всё
Hello
Hello!
Код: Выделить всё
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
ru.alliance.spring
spring-mvc-app
war
0.0.1-SNAPSHOT
spring-mvc-app Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
org.springframework
spring-core
5.2.24.RELEASE
org.springframework
spring-context
5.2.24.RELEASE
org.springframework
spring-web
5.2.24.RELEASE
org.springframework
spring-webmvc
5.2.24.RELEASE
org.springframework
spring-beans
5.2.24.RELEASE
org.thymeleaf
thymeleaf-spring5
3.1.2.RELEASE
javax.servlet
javax.servlet-api
4.0.1
provided
spring-mvc-app
введите здесь описание изображения
Я использую URL: http://localhost:8080/spring-mvc- app/hello.
Я пытался настроить пути в сборке веб-развертывания, но это не привело к успеху. Подобные проблемы я нашел в Интернете, но данные решения меня не устроили.
Подробнее здесь: https://stackoverflow.com/questions/787 ... in-eclipse