Answer the question
In order to leave comments, you need to log in
Java Spring MVC does not see views, why (Inteliji Idea)?
Engaged in the Spring Framework on the videos of this channel https://www.youtube.com/channel/UCK5d3n3kfkzlArMcc.... The first 14 sessions went great and there were no problems that I could not fix. At the 15th-16th lesson, the creation of an MVC project was discussed using two methods (configuration through an xml file and through a java class). Already on the 15th lesson there were problems. I did everything exactly the same as the teacher, but I had errors that were not in the video (errors related to slf4j). It seems that I solved this problem, but when I start the project, I don’t get the view that I indicated for opening. I've been trying to solve this problem for 2 days, but it doesn't work. When I deleted the index.jsp file (the file that opens instead of the one I specified), a page appeared reporting a 404 error. The same problems occurred during lesson 16.
I uploaded the code to github, I hope it helps.
https://github.com/Giksengik/spring/tree/main/MVC/...
Configuration Class:
package ru.vlasov.MVCFirstApp.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(basePackages = "ru.vlasov.MVCFirstApp")
@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.vlasov.MVCFirstApp.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MySpringMvcDispatcherSerlvetIntitializer 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.vlasov.MVCFirstApp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/")
public class HelloController {
@GetMapping("/hello-world")
public String sayHello() {
return "hello_world";
}
}
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/temp4/log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Answer the question
In order to leave comments, you need to log in
Hello colleague!
I have looked at your repository and can guess the following reasons why you are unable to use thymeleaf.
1) You have 2 modules and both modules are war. Accordingly, we can assume that you are running the first module (firstapp), instead of the second (MVCFirstApp). And thymeleaf template engine is connected in the second module.
So you need to run the second module through Tomcat. I also recommend reading about maven modules in order to understand how they are arranged and how they work. For example, in the parent pom.xml, you need to specify packaging - pom, for modules that are a library - jar, and for the main module that contains application launch files - specify war or jar, depending on what you need.
2) The second reason why you can't open the page could be in the controller. It is possible that you are trying to open the template via the base URL (/) and the method to which the template is assigned is /hello-world
Your code snippet?:
@Controller
@RequestMapping("/")
public class HelloController {
@GetMapping("/hello-world")
public String sayHello() {
return "hello_world";
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question