org.jolab.springrest.config.WebConfig.java Source code

Java tutorial

Introduction

Here is the source code for org.jolab.springrest.config.WebConfig.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jolab.springrest.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * Spring Web Configuration class.
 * Extends WebConfigurerrAdapter to 
 * Enable MVC pattern
 * Enalble component scanning in specific package, Discover automatically 
 * @Controller annotated classes. So Controller classes doesn't must be defined 
 * in configuration class.
 * 
 * @author jolab
 */
@Configuration
@EnableWebMvc
@ComponentScan("org.jolab.springrest")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        // Default configurer is enabled to handle static resource request 
        // forwarding them to the container's default servlet
        configurer.enable();

    }

}