0

использование JSP шаблонизатора

This commit is contained in:
2018-09-23 18:02:22 +03:00
parent b4f4ab89c7
commit 5af7dfc524
4 changed files with 29 additions and 6 deletions

View File

@@ -1,9 +1,11 @@
# Skeleton: SpringMVC (war) # Skeleton: SpringMVC (war) + JSP template engine
В данной ветке располагается пример *(заготовка, шаблон)* В данной ветке располагается пример *(заготовка, шаблон)*
для создания _WebApplication_ средствами **Spring Framework** для создания _WebApplication_ средствами **Spring Framework**
с использованием **Spring WebMVC**. с использованием **Spring WebMVC**.
В качестве шаблонизатора использован **JSP**.
## Сборка ## Сборка
``` ```

View File

@@ -1,11 +1,13 @@
package ru.dmitriymx.skeleton.springmvc; package ru.dmitriymx.skeleton.springmvc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration @Configuration
@EnableWebMvc @EnableWebMvc
@@ -13,6 +15,14 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class SpringConfigMVC implements WebMvcConfigurer { public class SpringConfigMVC implements WebMvcConfigurer {
private static final int ONE_YEAR = 365*24*60*60; private static final int ONE_YEAR = 365*24*60*60;
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/templates/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(ONE_YEAR); registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(ONE_YEAR);

View File

@@ -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";
} }
} }

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Skeleton - SpringMVC</title>
</head>
<body>
<h1>JSP template engine</h1>
<p>${message}</p>
</body>
</html>