Шаблонизатор Thymeleaf
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -71,6 +71,12 @@
|
|||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>1.16.16</version>
|
<version>1.16.16</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.thymeleaf</groupId>
|
||||||
|
<artifactId>thymeleaf-spring4</artifactId>
|
||||||
|
<version>3.0.9.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package ru.dmitriymx.skeleton.springmvc;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
import org.thymeleaf.TemplateEngine;
|
||||||
|
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||||
|
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
|
||||||
|
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||||
|
import org.thymeleaf.templatemode.TemplateMode;
|
||||||
|
import org.thymeleaf.templateresolver.ITemplateResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Данный класс является файлом настройки контекста для Spring
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableWebMvc
|
||||||
|
public class SpringConfigMVC extends WebMvcConfigurerAdapter{
|
||||||
|
@Autowired
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
private ITemplateResolver templateResolver(){
|
||||||
|
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
|
||||||
|
resolver.setApplicationContext(applicationContext);
|
||||||
|
resolver.setPrefix("classpath:/templates/");
|
||||||
|
resolver.setTemplateMode(TemplateMode.HTML);
|
||||||
|
resolver.setCacheable(true);
|
||||||
|
resolver.setSuffix(".html");
|
||||||
|
return resolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TemplateEngine templateEngine(){
|
||||||
|
SpringTemplateEngine engine = new SpringTemplateEngine();
|
||||||
|
engine.setTemplateResolver(templateResolver());
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Компонент, отвечающий за отображение шаблонов
|
||||||
|
*
|
||||||
|
* @return {@link ViewResolver}
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public ViewResolver viewResolver() {
|
||||||
|
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
||||||
|
viewResolver.setTemplateEngine(templateEngine());
|
||||||
|
viewResolver.setContentType("text/html;charset=UTF-8");
|
||||||
|
viewResolver.setCache(true);
|
||||||
|
return viewResolver;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,13 +7,10 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
|
|||||||
import org.eclipse.jetty.servlet.ServletHolder;
|
import org.eclipse.jetty.servlet.ServletHolder;
|
||||||
import org.eclipse.jetty.util.log.Log;
|
import org.eclipse.jetty.util.log.Log;
|
||||||
import org.eclipse.jetty.util.log.Slf4jLog;
|
import org.eclipse.jetty.util.log.Slf4jLog;
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.web.context.ContextLoaderListener;
|
import org.springframework.web.context.ContextLoaderListener;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
import org.springframework.web.servlet.DispatcherServlet;
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
@@ -23,15 +20,6 @@ public class WebApp {
|
|||||||
private final String host;
|
private final String host;
|
||||||
private final int port;
|
private final int port;
|
||||||
|
|
||||||
/**
|
|
||||||
* Данный класс является файлом настройки контекста для Spring
|
|
||||||
* однако, т.к. мы никакие бины не объявляем, то красс пустует
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
@EnableWebMvc
|
|
||||||
public static class SpringConfigMVC extends WebMvcConfigurerAdapter {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Создаем Spring-контекст
|
* Создаем Spring-контекст
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package ru.dmitriymx.skeleton.springmvc;
|
package ru.dmitriymx.skeleton.springmvc;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Здесь описываются все пути, что начинаются с '/'
|
* Здесь описываются все пути, что начинаются с '/'
|
||||||
@@ -14,11 +14,11 @@ public class WebAppController {
|
|||||||
/**
|
/**
|
||||||
* Обработка корневого запроса '/'
|
* Обработка корневого запроса '/'
|
||||||
*
|
*
|
||||||
* @return Сырое строковое значение
|
* @return Название шаблона
|
||||||
*/
|
*/
|
||||||
@RequestMapping
|
@RequestMapping
|
||||||
@ResponseBody
|
public String index(Model model) {
|
||||||
public String index() {
|
model.addAttribute("message", "Hello world!");
|
||||||
return "Hello world!";
|
return "index";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/main/resources/templates/index.html
Normal file
11
src/main/resources/templates/index.html
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>Skeleton - SpringMVC</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Thymeleaf template engine</h1>
|
||||||
|
<p th:text="${message}">message</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user