I am trying to set home page of my application by using spring boot.. but I am getting the error as Could not resolve view with name 'index.html' in servlet with name 'dispatcherServlet'
My code is as follow
RunApplication.java
@SpringBootApplication
public class RunApplication {
static Logger logger = Logger.getLogger(RunNavneetApplication.class);
public static void main(String[] args) {
SpringApplication.run(RunNavneetApplication.class, args);
}
}
application.properties
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.html
HomeController.java
@Controller
public class HomeController {
@RequestMapping("/")
public String index() {
return "index";
}
}
WebConfig.java
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/productImages/**")
.addResourceLocations("file:/home/rahul/Desktop/product_images/")
.setCachePeriod(0);
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wocs</groupId>
<artifactId>REST</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java-version>1.8</java-version>
<service-version>0.1.36-SNAPSHOT</service-version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.wocs</groupId>
<artifactId>services</artifactId>
<version>${service-version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
Please help me to fix this issue.. Thanks a lot in advance...
For me (spring-boot-starter-parent version 2.0.5.RELEASE) it worked changing the path in this way:
src/main/resources/
WEB-INF/view/index.html
→
src/main/resources/
META-INF/resources/view/index.html
(for some reason it seemed not liking the WEB-INF
name and it needed the nested resources
directory).
It seems to work also with src/main/
resources/resources/view/index.html
In these cases you don't even need to add the @EnableWebMvc
annotation and implement the WebMvcConfigurer
(prefix and suffix configured in application.properties
are enough).
I add to my answer also a clarification about the MVC configuration taken from the Spring Boot Reference Guide:
27.1.1 Spring MVC Auto-configuration
If you want to keep Spring Boot MVC features and you want to add additional MVC configuration [...] you can add your own
@Configuration
class of typeWebMvcConfigurer
but without@EnableWebMvc
.If you want to take complete control of Spring MVC, you can add your own
@Configuration
annotated with@EnableWebMvc
.76.7 Switch off the Default MVC Configuration
The easiest way to take complete control over MVC configuration is to provide your own
@Configuration
with the@EnableWebMvc
annotation. Doing so leaves all MVC configuration in your hands.
Removing of @EnableWebMvc annotation works for me in Spring Boot Project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With