Шаблонизатор Freemarker
This commit is contained in:
10
pom.xml
10
pom.xml
@@ -52,6 +52,11 @@
|
|||||||
<artifactId>spring-webmvc</artifactId>
|
<artifactId>spring-webmvc</artifactId>
|
||||||
<version>${spring.version}</version>
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context-support</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- JETTY -->
|
<!-- JETTY -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -71,6 +76,11 @@
|
|||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>1.16.16</version>
|
<version>1.16.16</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.freemarker</groupId>
|
||||||
|
<artifactId>freemarker</artifactId>
|
||||||
|
<version>2.3.23</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package ru.dmitriymx.skeleton.springmvc;
|
||||||
|
|
||||||
|
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.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||||
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Данный класс является файлом настройки контекста для Spring
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableWebMvc
|
||||||
|
public class SpringConfigMVC extends WebMvcConfigurerAdapter {
|
||||||
|
/**
|
||||||
|
* Компонент, отвечающий за отображение шаблонов
|
||||||
|
*
|
||||||
|
* @return {@link ViewResolver}
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public ViewResolver viewResolver() {
|
||||||
|
FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
|
||||||
|
viewResolver.setContentType("text/html;charset=UTF-8");
|
||||||
|
viewResolver.setCache(true);
|
||||||
|
viewResolver.setSuffix(".ftl");
|
||||||
|
return viewResolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Настройки для Freemarker
|
||||||
|
*
|
||||||
|
* @return {@link FreeMarkerConfigurer}
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public FreeMarkerConfigurer freemarkerConfig() {
|
||||||
|
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
|
||||||
|
freeMarkerConfigurer.setTemplateLoaderPath("classpath:/templates/");
|
||||||
|
return freeMarkerConfigurer;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/main/resources/templates/index.ftl
Normal file
12
src/main/resources/templates/index.ftl
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[#ftl]
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>Skeleton - SpringMVC</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Freemarker template engine</h1>
|
||||||
|
<p>${message}</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user