I have swtich from a spring MVC working application configured with xml configuration for java with spring loading, and I came across the following behavior. No matter which jsp page I call, I always get a 404 error. The controller hits because I see sysout messages from the controller's printer on the console, but the view is not displayed. I explicitly set the dispatcher display to "/" not "/ *", but it still does not work.
What I also see and donโt know if this is abnormal is the conclusion "There is no mapping found for the HTTP request with the URI [/WEB-INF/jsp/profile6.jsp] in the DispatcherServlet named" dispatcherServlet ""
By normal, I mean that I registered a dispatcher with the name "dispatcher" and not "dispatcherServer". It seems to me that the dispatcher is registered incorrectly, since the sysout "STARTING THE CONFIGURATION" in onStartup is also not printed. Then there is some โServlet Managerโ that is able to print sysouts from the controller, but the view was not found.
Here is the main spring boot function:
@ComponentScan("org.syncServer")
@Configuration
@EnableWebMvc
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("PRINTING the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
}
Here is the initializer that extends the WebApplicationInitializer class according to the munal:
public class Initializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
System.out.println("STARTING THE CONFIGURATION");
AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
mvcContext.register(MVCConfig.class);
mvcContext.refresh();
ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
"dispatcher", new DispatcherServlet(mvcContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
MVC Context:
@Configuration
@ComponentScan(basePackages = "org.syncServer")
@EnableWebMvc
public class MVCConfig extends WebMvcConfigurerAdapter{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pdfs/**").addResourceLocations("/pdfs/");
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
my controller:
@Controller
public class ProfileController {
@RequestMapping(value = "/profile6", method=RequestMethod.GET)
public String profile6 (){
System.out.println("profile hellow6 has been hit");
return "profile6";
}
jsp profile page profile6
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>this is the greeting jsp profile6 </title>
</head>
<body>
<h1>Profile6</h1>
spring boot output with tomcat 8.3
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.0.0.RC3)
.......
.......
2014-02-24 15:23:11.644 INFO 6038
2014-02-24 15:23:12.028 INFO 6038
2014-02-24 15:23:12.029 INFO 6038
2014-02-24 15:23:12.171 INFO 6038
2014-02-24 15:23:12.172 INFO 6038
2014-02-24 15:23:12.809 INFO 6038
2014-02-24 15:23:18.749 INFO 6038
2014-02-24 15:23:18.749 INFO 6038
2014-02-24 15:23:18.749 INFO 6038
2014-02-24 15:23:18.750 INFO 6038
2014-02-24 15:23:18.750 INFO 6038
2014-02-24 15:23:18.750 INFO 6038
2014-02-24 15:23:18.788 INFO 6038
2014-02-24 15:23:18.789 INFO 6038
2014-02-24 15:23:19.372 INFO 6038
2014-02-24 15:23:19.448 INFO 6038
2014-02-24 15:23:19.461 WARN 6038
2014-02-24 15:23:19.614 INFO 6038
2014-02-24 15:23:19.818 DEBUG 6038
2014-02-24 15:23:19.823 DEBUG 6038
2014-02-24 15:23:34.702 INFO 6038
2014-02-24 15:23:34.703 INFO 6038
2014-02-24 15:23:34.725 INFO 6038
INDEX INDEX has been hit
2014-02-24 15:23:34.853 WARN 6038
2014-02-24 15:23:34.980 WARN 6038
2014-02-24 15:23:34.986 WARN 6038
profile hellow6 has been hit
2014-02-24 15:23:38.423 WARN 6038
and pom file
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>1.0.0.RC3</version>
</parent>
<groupId>sync</groupId>
<artifactId>syncServer</artifactId>
<name>syncServer</name>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat8.version}</version>
</dependency>
</dependencies>
<version>1.0-SNAPSHOT</version>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<stopPort>9966</stopPort>
<stopKey>foo</stopKey>
<scanIntervalSeconds>10</scanIntervalSeconds>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-Xlint:unchecked</compilerArgument>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>archetype</id>
<activation>
<file>
<exists>archetype.properties</exists>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<repositories>
<repository>
<id>jboss-public-repository</id>
<url>https://repository.jboss.org/nexus/content/repositories/public</url>
</repository>
<repository>
<id>springsource-repo</id>
<name>SpringSource Repository</name>
<url>http://repo.springsource.org/release</url>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
<properties>
<java-version>1.7</java-version>
<spring-version>4.0.1.RELEASE</spring-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
<start-class>org.syncServer.core.Application</start-class>
<springBootVersion>1.0.0</springBootVersion>
<tomcat8.version>8.0.3</tomcat8.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<packaging>jar</packaging>
</project>