0

Running TomCat in Eclipse. I type in the browser http://localhost:8080/springmvc/index.jsp and see " Hello World!".

But if I type http://localhost:8080/springmvc/hello, I see a 404 error

What am I doing wrong?

My Controller

@Controller
public class WebController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

My Config

@Configuration
@ComponentScan("ru.neutrino")
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    private final ApplicationContext applicationContext;

    @Autowired
    public WebConfig(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);
    }
}
Jeroen Steenbeeke
  • 3,884
  • 5
  • 17
  • 26
  • Which versions of Tomcat and Spring are you using? – Piotr P. Karwasz Jun 12 '21 at 06:05
  • If you are learning Spring, don't use all this obsolete setup: Use Spring Boot instead, and you can start with a ready-to-go project at https://start.spring.io, no external Tomcat needed. Note that the setup you show is configured for _Thymeleaf_, not JSP, which is a better template technology. – chrylis -cautiouslyoptimistic- Jun 12 '21 at 06:46
  • @PiotrP.Karwasz using Tomcat v.10 – Александр Чевтаев Jun 13 '21 at 11:41
  • Does this answer your question? [Deploying Spring MVC 5 on Tomcat 10 ... deployment problems](https://stackoverflow.com/questions/66217350/deploying-spring-mvc-5-on-tomcat-10-deployment-problems) – Piotr P. Karwasz Jun 13 '21 at 15:18
  • @PiotrP.Karwasz I use annotations, but if I delete web.xml when the server starts, Tomcat automatically creates a web.xml with this content < display name>spring mvc index.html< / welcome file> index.htm< / welcome file> index. jsp< / welcome file> default.html< / welcome file> default.htm< / welcome file> default. jsp< / welcome file> – Александр Чевтаев Jun 14 '21 at 05:31

1 Answers1

-1

You are missing @RequestMapping annotation at the controller class level.

@Controller
@RequestMapping("/web")
public class WebController {

   @GetMapping("/hello")
   public String hello() {
      return "hello";
   }
 }

Try accessing http://localhost:8080/springmvc/web/hello

Sidharth
  • 152
  • 4