LoginSignup
9
9

More than 5 years have passed since last update.

DropwizardでThymeleaf,Bootstrap

Last updated at Posted at 2014-05-16

Dropwizardのview

こちらに説明があります。
FreeMarkerとmustacheしかなかったので、Springと仲の良いThymeleafを使えるようにしてみた。
https://github.com/deffence1776/dropwizard-views-thymeleaf

Thymeleafはテンプレートが通常のHTMLとして、表示できるのがいいところ。JavaScriptなんかもアプリを通さず利用できて欲しいところなので、やってみた。

Application

ExampleBootStrapApplication.java
public class ExampleBootstrapAppication extends Application<ExampleBootstrapConfigration>{

    public static void main(String[] args) throws Exception {
        new ExampleBootstrapAppication().run(args);
    }

    @Override
    public void initialize(Bootstrap<ExampleBootstrapConfigration> bootstrap) {

        //ThymeleafviewRenderer
        ImmutableSet<ViewRenderer> renderes= ImmutableSet.of((ViewRenderer)new ThymeleafViewRenderer());
        bootstrap.addBundle(new ViewBundle(renderes));

       //静的ファイルの設定。
       //ThymeleafViewRendererのtemplateファイル配置場所を設定
        bootstrap.addBundle(new AssetsBundle("/templates/css","/css",null,"css"));
          bootstrap.addBundle(new AssetsBundle("/templates/js","/js",null,"js"));
          bootstrap.addBundle(new AssetsBundle("/templates/fonts","/fonts",null,"fonts"));

    }

    @Override
    public void run(ExampleBootstrapConfigration configuration, Environment environment)
            throws Exception {

        environment.jersey().register(new ExampleBootstrapResource());

    }
}

Configuration

ExampleBootstrapConfigration.java
public class ExampleBootstrapConfigration extends Configuration {

    @JsonProperty
    private String appName;

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }
}

Resource,View

ExampleBootstrapResource.java
@Path("/")
public class ExampleBootstrapResource {

    @GET
    public ExampleBootstrapView index(){
        return new  ExampleBootstrapView();
    }

}
ExampleBootstrapView.java
public class ExampleBootstrapView extends ThymeleafView{
    private String value;

    protected ExampleBootstrapView() {
        super("index.html");
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

templateと静的ファイル

src/resources/templates
|-index.html(BootStrapのテンプレート)
|-css
|-fonts
|-js

9
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9