Package com.sivalabs.springapp.web.config

Source Code of com.sivalabs.springapp.web.config.WebMvcConfig

/**
*
*/
package com.sivalabs.springapp.web.config;

import java.util.Properties;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
* @author Siva
*
*/
@Configuration
@ComponentScan(basePackages = { "com.sivalabs.springapp.web"})
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter
{

  @Override
  public void addViewControllers(ViewControllerRegistry registry)
  {
    super.addViewControllers(registry);
    //registry.addViewController("").setViewName("welcome"); 
    registry.addViewController("login/form").setViewName("login");   
    registry.addViewController("welcome").setViewName("welcome");
    registry.addViewController("admin").setViewName("admin");
  }

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

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry)
  {
    registry.addResourceHandler("/app/resources/**").addResourceLocations("/resources/");
  }

  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
  {
    configurer.enable();
  }

  @Bean(name = "messageSource")
  public MessageSource configureMessageSource()
  {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:messages");
    messageSource.setCacheSeconds(5);
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
  }

  @Bean
  public SimpleMappingExceptionResolver simpleMappingExceptionResolver()
  {
    SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();
    Properties mappings = new Properties();
    mappings.put("org.springframework.dao.DataAccessException", "error");
    b.setExceptionMappings(mappings);
    return b;
  }
}
TOP

Related Classes of com.sivalabs.springapp.web.config.WebMvcConfig

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.