Error on the web page when launching the application on Spring MVC in Eclipse: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
I recently started studying Spring MVC and still can’t launch the web application, it gives an error in the browser. I’m launching it via Tomcat.
Here are my files:
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);
}
}
Controller:
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";
}
}
html files:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>GoodBye</title>
</head>
<body>
GoodBye!
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
Hello!
</body>
</html>
Folder hierarchy:
enter image description here
I tried to set up paths in the Web Deployment Assembly, but this did not lead to success. I found similar problems on the Internet, but those solutions did not suit me.
Pavel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.