2011년 12월 7일 수요일

Spring 3.x Annotation WebApplicaitonInitializer / without web.xml


package com.domain.config;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

AnnotationConfigWebApplicationContext rootContext =
                                           new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);

servletContext.addListener(new ContextLoaderListener(rootContext));

AnnotationConfigWebApplicationContext dispatcherContext =
                                           new   AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherConfig.class);

ServletRegistration.Dynamic dispatcher =
                       ServletContext.addServlet("dispatcher", new  DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}

}



package com.domain.config;

public class AppConfig {

}


package com.domain.config;



import java.util.List;

import javax.inject.Inject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.core.env.Environment;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;


@Configuration
@EnableWebMvc
@ComponentScan (basePackages = "com.domain")
public class DispatcherConfig extends WebMvcConfigurerAdapter {

@Inject
private Environment environment;
private final Log logger = LogFactory.getLog(getClass());
// implementing WebMvcConfigurer

@Bean 
public ViewResolver viewResolver (){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
//-----------------------------------------//
// Configures Tiles at application startup //
//-----------------------------------------//
@Bean
public TilesConfigurer tilesConfigurer(){
TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions(new String[]{
 "/WEB-INF/layouts/tiles.xml"
//  "/WEB-INF/views/**/tiles.xml"
});
configurer.setCheckRefresh(true);
return configurer;
}

//-------------------------------------------------------//
// Messages to support internationalization/localization //
//-------------------------------------------------------//
@Bean
public MessageSource messageSource (){
ReloadableResourceBundleMessageSource messageSource 
                 = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/WEB-INF/messages/messages");
if (environment.acceptsProfiles("embedded")){
messageSource.setCacheSeconds(0);
}
return messageSource;
}

public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
public Validator getValidator() {
logger.info("Validation check ---------------------");
LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
ReloadableResourceBundleMessageSource messageSource = 
                                                                          new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/WEB-INF/messages/validation");
if (environment.acceptsProfiles("embedded")) {
messageSource.setCacheSeconds(0);
}
logger.info(messageSource);
factory.setValidationMessageSource(messageSource);
return factory;
}
// com.springsource.org.codehaus.jackson
// com.springsource.org.codehaus.jackson.mapper
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJacksonHttpMessageConverter());
}
}


package com.domain.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.domain.data.UserImpl;

@Controller
public class HomeController {

@RequestMapping (value="/", method=RequestMethod.GET)
public ModelAndView Home (){
ModelAndView model = new ModelAndView ("Home");
model.addObject("message","messageObject");
return model;
}
}


WebContent/WEB-INF/views/Home.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"  pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    HomeController Message get  ${message}
    <h1>Welcome Spring Annotation Base Web Configuration </h1>
</body>
</html>


댓글 없음:

댓글 쓰기