Spring framework 3 xml files to java files

ServletContextXml.java


package com.test.settings;

import java.io.IOException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
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.mvc.WebContentInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;


@Configuration
@ComponentScan("com.test")
@EnableWebMvc // <mvc:annotation-driven />
public class ServletContextXml extends WebMvcConfigurerAdapter {
   
       
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
   
    @Bean
    public InternalResourceViewResolver setInternalResourceViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
       
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }  
}





WebXml.java

package com.test.settings;
 
import javax.servlet.FilterRegistration;
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.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
 
public class WebXml implements WebApplicationInitializer{
 
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(ServletContextXml.class);
        servletContext.addListener(new ContextLoaderListener(ctx));
        
        FilterRegistration.Dynamic charEncodingFilter = 
                servletContext.addFilter("charEncodingFilter", getCharacterEncodingFilter());
    
        ctx.setServletContext(servletContext);
        
        ServletRegistration.Dynamic servlet = 
                servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        
       charEncodingFilter.addMappingForServletNames(null, true, servlet.getName());
        
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }
    
    private CharacterEncodingFilter getCharacterEncodingFilter(){
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);
        return filter;
    }
 
}

댓글

이 블로그의 인기 게시물

spring boot as a windows service

Properties

PHP Date format