Skip to content

Instantly share code, notes, and snippets.

@sivaprasadreddy
Created March 2, 2014 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivaprasadreddy/9302326 to your computer and use it in GitHub Desktop.
Save sivaprasadreddy/9302326 to your computer and use it in GitHub Desktop.
WebMvcConfig.java
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;
@Configuration
@ComponentScan(basePackages = { "com.sivalabs.springapp.web"})
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter
{
@Override
public void addViewControllers(ViewControllerRegistry registry)
{
super.addViewControllers(registry);
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("/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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment