Skip to content

Instantly share code, notes, and snippets.

@danilosilvadev
Last active June 1, 2017 17:43
Show Gist options
  • Save danilosilvadev/b0456ee9637e137d679f0cb52f7ab5ed to your computer and use it in GitHub Desktop.
Save danilosilvadev/b0456ee9637e137d679f0cb52f7ab5ed to your computer and use it in GitHub Desktop.
//FILLE 1- MAVEN
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
//Don't forget put:
<packaging>war</packaging>
//FILE 2 - A CLASS TO RESOLVE THE DispatcherServlet and ContextLoaderListener
public class SpitterWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
//Sets the root
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
//Sets the view resolver class
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
//This will map "/" to be between the paths.
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
//FILE 3 - RootConfig to sets the root of project
@Configuration //Sets this class to be a configuration class
@ComponentScan(basePackages={"project.controller"}) //Search for beans(DI), any class with @Component or @Controller will be a bean.
public class RootConfig {
}
//FILE 4 - WebConfig to resolver the views
@Configuration
@EnableWebMvc //Enable the MVC pattern at spring
@ComponentScan("project.controller")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/"); //That is the package where my views are.
resolver.setSuffix(".jsp"); //That is the extension of the files with the views.
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// TODO Auto-generated method stub
super.addResourceHandlers(registry);
}
}
//That's it! :D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment