Coder Social home page Coder Social logo

dropwizard-views-thymeleaf's Introduction

dropwizard-views-thymeleaf

Dropwizard のテンプレートエンジンとして、Thymeleaf Viewsを利用する

#What is Thymeleaf http://www.thymeleaf.org/

##Getting Started Dropwizard App:

	public class ExampleApplication extends Application<ExampleConfiguration> {

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


	@Override
	public void initialize(Bootstrap<ExampleConfiguration> bootstrap) {
		//set ThymeleafView Render
		ImmutableSet<ViewRenderer> renderes= ImmutableSet.of((ViewRenderer)new ThymeleafViewRenderer());
		bootstrap.addBundle(new ViewBundle(renderes));

	}

	@Override
	public void run(ExampleConfiguration configuration, Environment environment)
			throws Exception {
		environment.jersey().register(new ExampleResource());

	}  
}

you can change the template mode,cacheTTLs,template suffix and template prefix.

#View using ThymeleafView:

	public class PersonView extends ThymeleafView {
	
	private Person person;//model used in template
	private List<Person> persons;//model used in template


	protected PersonView(String templateName,Person p ,List<Person> pList) {
		super(templateName);
		person = p;
		persons =pList;
	}
	
	//add getter for use
	public Person getPerson() {
		return person;
	}
	
	//add getter for use
	public List<Person> getPersons() {
		return persons;
	}

  }

resource example:

package com.deffence.mm.dropwizard.views.thymeleaf.example.resources;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import com.google.common.collect.Lists;

@Path("/")
public class ExampleResource {

	@GET
	public PersonView getPersons(){

		List<Person> pList = Lists.newArrayList();
		pList.add(new Person("list1",10));
		pList.add(new Person("list2",10));


		return new PersonView("person.html" ,new Person("name",10), pList);
	}

}

#Template

default location: classpath:/templates/something.html
[/template/] is libraly default prefix. you can change default prefix by ThymeleafViewRenderer's constructor.

template example:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title th:text="#{title}">Welcome Page</title>
</head>
<body>
		<p>Welcome</p>
		<span th:text="#{name}" value="名前用" />
		<input type="text" th:value="${person.name}" value="ユーザさん" />
		<span th:text="#{age}" value="年齢用" />
		<input type="text" th:value="${person.age}" value="?才" />
		<table border="1">
			<tr>
				<th><span th:text="#{name}" value="名前用" /></th>
				<th><span th:text="#{age}" value="年齢用" /></th>
			</tr>
			<tr th:each="p : ${persons}">
				<td th:text="${p.name}"></td>
				<td th:text="${p.age}"></td>
			</tr>
		</table>
	<div th:include="/footer.html::copy"></div>
</body>
</html>

#Assets use static files https://github.com/deffence1776/dropwizard-views-thymeleaf/tree/master/dropwizard-views-thymeleaf-example

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) {
        ImmutableSet<ViewRenderer> renderes= ImmutableSet.of((ViewRenderer)new ThymeleafViewRenderer());
        bootstrap.addBundle(new ViewBundle(renderes));

        //configure assets。
       bootstrap.addBundle(new AssetsBundle("/templates","/"));
    }

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

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

    }
}
classpath:templates---index.html(templateHTMLfile)
		    |-css
		    |-fonts
		    |-js

dropwizard-views-thymeleaf's People

Contributors

deffence1776 avatar justinwoo avatar

Watchers

Satoshi Takata avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.