I am trying to configure Spring Boot using annotations. I have class
@EnableWebMvc
@Configuration
@ComponentScan({
...
})
@EnableTransactionManagement
@EnableAutoConfiguration
@Import({ SecurityConfig.class })
public class AppConfig extends SpringBootServletInitializer {...}
which contains this View resolver which works fine.
@Bean
public InternalResourceViewResolver internalViewResolver() {
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
viewResolver.setOrder(1);
return viewResolver;
}
But after receiving name of JSP file application raise this error: No mapping found for HTTP request with URI [/WEB-INF/pages/MainPage.jsp] in DispatcherServlet with name 'dispatcherServlet'.
I found solution for XML configuration:
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
But I am using annotation configuration, so this soultion is not suitable for me.
I tried to resolve this problem extending AbstractAnnotationConfigDispatcherServletInitializer
public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
// I thought this method will be equivalent to XML config solution described above
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
But nothing has changed after this. By the way I looked at few examples with AbstractAnnotationConfigDispatcherServletInitializer, but I still don't understand how application can use this class when it's not annotated and no instances of this class are created. It's just declared and that's all. Maybe I need to create an instance of this class and attach it anywhere?
Anyway I see in log this line: Mapping servlet: 'dispatcherServlet' to [/] So it looks like I have right servlet configuration.
I tried this solution but it doesn't help. I removed InternalResourceViewResolver and created application.properties with such content:
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
But after that I received: javax.servlet.ServletException: Could not resolve view with name 'MainPage' in servlet with name 'dispatcherServlet'
So what is the proper way to resolve this problem?
UPDATE I tried to create a new simple project from scratch.
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>edu.springtest</groupId>
<artifactId>SpringTest</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.0.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Main.java:
@Controller
@Configuration
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
public class Main {
@RequestMapping("/")
String home() {
return "hello";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Main.class, args);
}
}
application.properties:
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
Project's structure:
I run project with command mvn spring-boot:run and receive edu.test.Main: Started Main in 2.365 seconds (JVM running for 5.476) in output. But when I'm opening localhost:8080 I receive:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Dec 20 11:25:29 EET 2014
There was an unexpected error (type=Not Found, status=404).
No message available
But now I'm not receiving "No mapping found..." error in output. When I am opening localhost:8080 nothing is printed to output at all. So what am I doing wrong?