SpringFramework + Thymeleaf での画面作成#002 [SpringFramework]

■SpringFramework でのMVC実装
■ web.xml を利用しないTomcatアプリの実装
 
 上記について、やってみました。
 View 部分は、Thymeleaf を採用します。
 
 とても参考にさせた頂いたサイトは、こちら 
 
 作成したEclipseの構成は、下図のようになります。
 プロジェクト名、ProtoWebService 略して(pws) 
  

  
 ポイントは、@Configuration を指定した初期設定用クラス
  jp.co.proto.pws.config.WebAppConfig
         
package jp.co.proto.pws.config;

import javax.annotation.Resource;

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.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring3.SpringTemplateEngine;
import org.thymeleaf.spring3.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import jp.co.proto.pws.Application;

/**
* Spring フレームワークの設定
*
*/
@Configuration
@EnableWebMvc
@Import({DbConfig.class})  //データベース設定をインポート
@ComponentScan(basePackageClasses = Application.class)
@PropertySource("classpath:resources/app.properties")
public class WebAppConfig extends WebMvcConfigurerAdapter {

@Resource
private Environment env;

//静的リソースの設定
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

//テンプレートリゾルバーの設定
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
//NB, selecting HTML5 as the template mode.
resolver.setTemplateMode("HTML5");
resolver.setCacheable(false);
resolver.setCharacterEncoding("UTF-8");
return resolver;
}

//Thymeleaf テンプレートエンジンの設定
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
return engine;
}

//Thymeleaf ビューリゾルバー設定
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(1);
viewResolver.setViewNames(new String[]{"*"});
viewResolver.setCache(false);
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}

//メッセージソースの設定
//WEBページでプロパティファイルを使用できる
//日本語メッセージ:messages_ja.properties
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource
        = new ReloadableResourceBundleMessageSource();
messageSource.setBasename(env.getRequiredProperty("message.source.basename"));
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(0);
return messageSource;
}
}

   正直、ほとんど上記サイトのコピペなのですが、
  
 @ComponentScan(basePackageClasses = Application.class)
 この行は、独自です。 
 jp.co.proto.pws 直下に配置している Application を指定することで
 すべてのJavaソースを ComponentScan の対象としています。
 
 @PropertySource("classpath:resources/app.properties") 
 →記述内容で想像がつくと思いますが、Javaのclasspath内に resourcesフォルダを作成し
  その中に app.properties というテキストファイルを配置します。
  このPropertieファイルへDBの接続情報などを記載します。
 
  Propertieの設定内容は、
  org.springframework.core.env.Environmentクラスの getRequiredProperty で String として
  取得可能です。 
 
  こんな感じ
  \workspace\ProtoWebSerice\src\resources
   #message source
message.source.basename=classpath:resources/messages

#master properties:
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/master
db.username=test
db.password=admin

#tran DB properties:
kd.db.driver=com.mysql.jdbc.Driver
kd.db.url=jdbc:mysql://127.0.0.1:3306/tran
kd.db.username=test
kd.db.password=admin

#Hibernate Configuration:
hibernate.hbm2ddl.auto=none
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
model.scan.package=jp.co.fourseeds.elm.model
 
 Properties参照クラスにて  
  @javax.annotation.Resource
     private Environment env;
   
  String someValue = env.getRequiredProperty("KEY"); 
 
 DBの接続情報が複数あるのは、複数のDBへ接続するときにDataSourceの指定方法を
 実装するためです。業務では、セキュリティ要件等でDBを物理的に分けるケースがあると
 思いますので、そうしたときの為の実験です。
 具体的には、次回以降に試します。 
 
 ところで、SpringFrameworkの基本は、公式サイトなどに詳細が記載されていますし、
 また、英語ですが YouTubeで、紹介されていたりもします。 

 

nice!(0)  コメント(0)  トラックバック(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

SpringFramework + Th..|- ブログトップ

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。