com.gantzgulch.sharing.configurationmvc.MvcAppConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.gantzgulch.sharing.configurationmvc.MvcAppConfig.java

Source

/*
 * Copyright 2011 GantzGulch, Inc.
 * 
 * This file is part of GantzFileSharing.
 * 
 * GantzFileSharing is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GantzFileSharing is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GantzFileSharing.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.gantzgulch.sharing.configurationmvc;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesView;

import com.gantzgulch.sharing.configuration.ApplicationContext;

@Configuration
@ComponentScan(basePackages = "com.gantzgulch.sharing.controller", excludeFilters = @ComponentScan.Filter(Configuration.class))
@EnableWebMvc
public class MvcAppConfig extends WebMvcConfigurerAdapter {

    private static final Log LOG = LogFactory.getLog(MvcAppConfig.class);

    @Autowired
    private ApplicationContext applicationContext;

    public MvcAppConfig() {
        LOG.debug("ctor");
    }

    @Override
    public void configureInterceptors(InterceptorConfigurer configurer) {
        try {
            OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
            interceptor.setSessionFactory(applicationContext.sessionFactory());
            interceptor.setFlushMode(OpenSessionInViewInterceptor.FLUSH_AUTO);
            configurer.addInterceptor(interceptor);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Bean(name = "multipartResolver")
    public MultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }

    @Bean(name = "tilesViewResolver")
    public ViewResolver tilesViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setViewClass(TilesView.class);
        resolver.setOrder(1);
        return resolver;
    }

    @Bean(name = "viewResolver")
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF");
        resolver.setOrder(2);
        return resolver;
    }

    @Bean(name = "tilesConfigurer")
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/tiles-defs.xml" });
        return tilesConfigurer;
    }
}