package jp.dip.arimodoki.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import jp.dip.arimodoki.common.CConst;
/**
* コンフィグレーションを外部ファイルから注入するクラスです
* 外部ファイルは、classpath:application.properties とします
*/
@Configuration
@PropertySource("classpath:application.properties")
public class WebConfig extends WebMvcConfigurerAdapter implements CConst {
/** リクエスト URL パターン */
@Value("${appconfig.url-pattern:/*}")
private String urlpattern;
/** 入力検証メッセージファイル */
@Value("${appconfig.validationmessage:classpath:/ValidationMessages}")
private String validationmessage;
/** ContentNegotiationConfigurer */
@Value("${appconfig.favorPathExtension:true}")
private boolean favorPathExtension;
@Value("${appconfig.ignoreAcceptHeader:true}")
private boolean ignoreAcceptHeader;
/** ファイルアップロードサイズ制限 */
@Value("${appconfig.MaxUploadSize:104857600}")
private long maxUploadSize;
@Value("${appconfig.MaxUploadSizePerFile:3145728}")
private long maxUploadSizePerFile;
/**
* 入力検証メッセージリソースファイルを設定します
* default: /*
*/
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename(this.validationmessage);
return messageSource;
}
/**
* リクエストURLマッピングパターンの登録(web.xml DispatcherServletのマッピングに相当)
* これを有効にすると、jsや、cssや、jpg などの拡張子のファイルが見えなくなてしまう。
* @param dispatcherServlet
* @return ServletRegistrationBean
*/
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
registration.setLoadOnStartup(1);
registration.addUrlMappings(this.urlpattern);
return registration;
}
/**
* ファイルアップロードサイズ制限
* default 最大サイズ:100M、1ファイルあたり最大サイズ:3M
* @return
*/
@Bean
public CommonsMultipartResolver createMultipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
resolver.setMaxInMemorySize((int)this.maxUploadSize);
resolver.setMaxUploadSize(this.maxUploadSize);
resolver.setMaxUploadSizePerFile(this.maxUploadSizePerFile);
return resolver;
}
/**
* ContentNegotiationConfigurerの初期設定
* @RestControllerのJSON返却 406問題対策
*/
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(this.favorPathExtension);
configurer.ignoreAcceptHeader(this.ignoreAcceptHeader);
}
/**
* (未使用)
* 静的ファイルのハンドラをトライしてみたけどできない ~~)
* 1)/main/resources/static/xxx.html ⇒ ×(だめ)
* 2)/static/xxx.html ⇒ 〇(これはOK)
* さまざまやってみたけど、結論としては
* プロジェクト直下の /static に
* /static/css とか、/static/js とか、/static/images とかを掘って静的コンテンツを置く
*/
public void addResourceHandlers(ResourceHandlerRegistry registry){
:
:
:
:
:
}
}
|