2013-01-12 12 views
13

私は、今のところ単純で基本的な春のWebアプリケーションを作成しました。私はデプロイメント記述子を単純なweb.xmlファイルとして、次にアプリケーションコンテキストをxmlファイルとして使用しています。Javaアプリケーションコンテキストでspring MVCの<mvc:resources>タグを使用するにはどうすればいいですか?

今、私はJavaファイルのみを使用して私の春のWebアプリケーション全体を作成しようとしたかったのですが。したがって、通常のデプロイメント記述子の代わりにWebApplicationInitializerを作成し、@Configurationアノテーションを使用するアプリケーションコンテキストを作成しました。

のデプロイメント・ディスクリプタ

package dk.chakula.config; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration.Dynamic; 
import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.WebApplicationContext; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.servlet.DispatcherServlet; 
/** 
* 
* @author martin 
* @since 12-1-2012 
* @version 1.0 
*/ 
public class Initializer implements WebApplicationInitializer { 

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

    private void registerDispatcherServlet(final ServletContext servletContext) { 
     WebApplicationContext dispatcherContext = createContext(ChakulaWebConfigurationContext.class); 
     DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext); 
     Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet); 
     dispatcher.setLoadOnStartup(1); 
     dispatcher.addMapping("/"); 
    } 

    private WebApplicationContext createContext(final Class<?>... annotatedClasses) { 
     AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
     context.register(annotatedClasses); 
     return context; 
    } 

} //End of class Initializer 

アプリケーションコンテキスト

package dk.chakula.config; 

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.view.UrlBasedViewResolver; 
import org.springframework.web.servlet.view.tiles2.TilesConfigurer; 
import org.springframework.web.servlet.view.tiles2.TilesView; 

/** 
* 
* @author martin 
* @since 12-01-2013 
* @version 1.0 
*/ 
@Configuration 
@EnableWebMvc 
@ComponentScan("dk.chakula.web") 
public class ChakulaWebConfigurationContext { 

    @Bean 
    public TilesConfigurer setupTilesConfigurer() { 
     TilesConfigurer configurer = new TilesConfigurer(); 
     String[] definitions = {"/layout/layout.xml"}; 
     configurer.setDefinitions(definitions); 
     return configurer; 
    } 

    @Bean 
    public UrlBasedViewResolver setupTilesViewResolver() { 
     UrlBasedViewResolver viewResolver = new UrlBasedViewResolver(); 
     viewResolver.setViewClass(TilesView.class); 
     return viewResolver; 
    } 

} //End of class ChakulaWebConfigurationContext 

私の問題は、私は方法を見つけるように見えることができないということですresourcesフォルダに自分のマッピングを '隔離' されイメージ、CSSのJavaScriptなどが含まれています。私のアプリケーションコンテキストがjavaにあるとき。私は/リソース/

<mvc:resources mapping="/resources/**" location="/resources/" /> 

私のWebアプリケーションは、私のイメージを使用することができますので、私は、これをどのように行うことができますへのマッピングを隔離するために、このタグを使用し、通常のXMLアプリケーションのコンテキストで

、CSSなど

+0

あなたはどのバージョンを使用していますか? –

+0

私は3.2.0を使用しています – Rohwedder

答えて

30

Spring MVCアプリケーションで静的リソースを処理できるようにするには、<mvc:resources/><mvc:default-servlet-handler/>という2つのXMLタグが必要です。 @EnableWebMvcアノテーションが使用されているので、直接WebMvcConfigurationSupportを拡張する必要がないことを

@Configuration 
@EnableWebMvc 
public class WebMvcConfig extends WebMvcConfigurerAdapter { 

    // equivalents for <mvc:resources/> tags 
    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(31556926); 
     registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(31556926); 
     registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926); 
    } 

    // equivalent for <mvc:default-servlet-handler/> tag 
    @Override 
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
     configurer.enable(); 
    } 

    // ... other stuff ... 
} 

注意し、あなただけのWebMvcConfigurerAdapterを拡張する必要があります:JavaベースのSpring構成で同じになります。詳細は、JavaDoc for @EnableWebMvcを参照してください。

+0

私の日私はWebMvcConfigurationSupportとWebMvcConfigurerAdapter.myの両方を拡張していました。 –

9

Web上でjavaのファイルだけを使用してSpring MVC 3に関する記事を検索した後、私はWebMvcConfigurationSupportクラスから拡張し、addResourceHandler(ResourceHandlerRegistry)とResourceHandlerMapping()の2つのメソッドをオーバーライドしてアプローチを使用しました。

私の新しいアプリケーションコンテキストは、このようになりました。

package dk.chakula.config; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.HandlerMapping; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; 
import org.springframework.web.servlet.handler.AbstractHandlerMapping; 
import org.springframework.web.servlet.view.UrlBasedViewResolver; 
import org.springframework.web.servlet.view.tiles2.TilesConfigurer; 
import org.springframework.web.servlet.view.tiles2.TilesView; 

/** 
* 
* @author martin 
* @since 12-01-2013 
* @version 1.0 
*/ 
@Configuration 
@EnableWebMvc 
@ComponentScan("dk.chakula.web") 
public class ChakulaWebConfigurationContext extends WebMvcConfigurationSupport { 

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

    @Override 
    @Bean 
    public HandlerMapping resourceHandlerMapping() { 
     AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) super.resourceHandlerMapping(); 
     handlerMapping.setOrder(-1); 
     return handlerMapping; 
    } 

    @Bean 
    public TilesConfigurer setupTilesConfigurer() { 
     TilesConfigurer configurer = new TilesConfigurer(); 
     String[] definitions = {"/layout/layout.xml"}; 
     configurer.setDefinitions(definitions); 
     return configurer; 
    } 

    @Bean 
    public UrlBasedViewResolver setupTilesViewResolver() { 
     UrlBasedViewResolver viewResolver = new UrlBasedViewResolver(); 
     viewResolver.setViewClass(TilesView.class); 
     return viewResolver; 
    } 

} //End of class ChakulaWebConfigurationContext 

私たちはレジストリに場所とリソースのマッピングを追加するには、addResourceHandlerを上書きしなければならなかった理解されているように。その後、HandlerMappingのオブジェクトを返したBeanが必要でした。私は春のドキュメントから読むことができるように、そして-1

HandlerMappingが静的 リソース要求を提供するためにInteger.MAX_VALUEの-1で注文を意味するので、このHandlerMappingの順序は、-1に設定する必要があります。

私のアプリケーションでは、cssファイルと画像をビューに読み込むことができるようになりました。答えを他の人に教えてもらいたいので、将来の人にはこの利点があります。

+3

多少面倒なことです!.. '@ EnableWebMvc'はすでに' WebMvcConfigurationSupport'を設定にインポートしているので、 '@ EnableWebMvc'アノテーションを使用したり、' WebMvcConfigurationSupport'同時に。詳細については、[JavaDoc for @EnableWebMvc](http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html)を参照してください。 –

4

はこれを試してみてください:

@Override 
    public void addResourceHandlers(final ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
    } 
関連する問題