Reputation: 1984
Having a confusing issue that may be entirely due to inexperience with both Spring and/or Intellj, but after searching across the internet and SO and the lot, was having no luck on how to fix so here goes.
In a nutshell, I have a sample application that I'm working on to gain some experience with Spring/Hibernate/Maven. It works fine (loads pages as expected, etc...), but in the code, I have a ton of flags for the IDE not able to resolve MVC views, which I can't see as to why this would be the case.
The setup is web.xmlless, using an entirely Java setup with the following Listener configuration as so.
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
registry.viewResolver(viewResolver);
}
and here one example of code where it flags that it cannot resolve the MVC view
@RequestMapping(value = { "/", "/list" }, method = RequestMethod.GET)
public String listUsers(ModelMap model) {
List<User> users = userService.findAllUsers();
model.addAttribute("users", users);
model.addAttribute("loggedinuser", getPrincipal());
return "userslist";
}
with userslist being the offending view not being found.
While I'm fairly certain that it's not a missing Spring plugin, the pom.xml has the following spring components, using version 4.2.5
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
Also using the latest version of IntellJ, 2016.2.4 that is registered.
Upvotes: 1
Views: 12207
Reputation: 1
<?xml version="1.0" encoding="UTF-8"?>
4.0.0 org.springframework.boot spring-boot-starter-parent 3.1.5 com.codepresso controllerexercise 0.0.1-SNAPSHOT controllerexercise controllerexercise <java.version>17</java.version> org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
</configuration>
</plugin>
</plugins>
</build>
Maybe you haven't added TEMPLATE ENGINES: Thymeleaf so there may be an error
Upvotes: 0
Reputation: 1
I had the @Configuration
class extend a base abstract class. The base abstract class did create the FreeMarkerConfigurer
and FreeMarkerViewResolver
objects.
Moving the creation of FreeMarkerConfigurer
and FreeMarkerViewResolver
from the abstract class to the class with the @Configuration
annotation solved the problem that IntelliJ couldn't find my freemarker template/views.
Upvotes: 0
Reputation: 319
If you are using IntelliJ IDEA, one possible cause for this issue could be the wrong Web Resource Directory. Go to File -> Project Structure -> Modules -> On the left side Web -> Web Resource Directories below configure the path to your web resource directory
Upvotes: 3
Reputation: 406
After spending around 4 hours on this, I found that this is an Intellij bug that is reported here.
The workaround is to configure view resolver like this:
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/pages/", ".jsp");
}
Upvotes: 2
Reputation: 1341
I think this issue occurs becouse Itellij does not properly recognize the ViewResolver Bean. Try changing the configuration of viewResolver to something like this:
@Configuration
@EnableWebMvc
@ComponentScan("com.app")
public class WebConfig extends WebMvcConfigurerAdapter{
@Bean
public InternalResourceViewResolver resolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
public void configureDefaultServerletHandling(DefaultServletHandlerConfigurer configurer){
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
Upvotes: 1
Reputation: 8262
Possible solution (I have to write an answer because of the screenshot):
You did not configure the facet for your module.
Open the Project Structure Dialog
go to the Modules entry on the left and add the Spring facet to it.
If you have done this you can add your spring configurations.
Idea should have notified you that there are unregistered Spring configs (something like this)
Upvotes: 4