1. Create a new Spring project ("Spring MVC Project") using STS in Eclipse.
2. Update the pom.xml to match the pom on the page of this blog.
3. Verify that the vanilla application runs on Tomcat in Eclipse. I am using an external install of Tomcat 7.0.50 as shown below.
If it runs successfully, you should see the page:
4. If successful, remove the Spring XML configuration files:
5. The next step is to create the two configuration files (Initializer.java, WebAppConfig.java) under the config directory below. The other files and directories are not relevant at this stage.
6. The files should look like the following:
package dtr.oas.config;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Order(1)
public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class< ? >[] getRootConfigClasses() {
return new Class[] { };
}
@Override
protected Class< ? >[] getServletConfigClasses() {
return new Class< ? >[] { WebAppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
package dtr.oas.config;
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.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("dtr.oas.controller")
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
7. Run the application on Tomcat again and you should see the success page again.
Code at GitHub: https://github.com/dtr-trading/spring-ex01




