использование JSP шаблонизатора
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
# Skeleton: SpringMVC (war)
|
||||
# Skeleton: SpringMVC (war) + JSP template engine
|
||||
|
||||
В данной ветке располагается пример *(заготовка, шаблон)*
|
||||
для создания _WebApplication_ средствами **Spring Framework**
|
||||
с использованием **Spring WebMVC**.
|
||||
|
||||
В качестве шаблонизатора использован **JSP**.
|
||||
|
||||
## Сборка
|
||||
|
||||
```
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package ru.dmitriymx.skeleton.springmvc;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@@ -13,6 +15,14 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
public class SpringConfigMVC implements WebMvcConfigurer {
|
||||
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
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(ONE_YEAR);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package ru.dmitriymx.skeleton.springmvc;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
/**
|
||||
* Здесь описываются все пути, что начинаются с '/'
|
||||
@@ -14,11 +14,11 @@ public class WebAppController {
|
||||
/**
|
||||
* Обработка корневого запроса '/'
|
||||
*
|
||||
* @return Сырое строковое значение
|
||||
* @return Название шаблона
|
||||
*/
|
||||
@RequestMapping
|
||||
@ResponseBody
|
||||
public String index() {
|
||||
return "Hello world!";
|
||||
public String index(Model model) {
|
||||
model.addAttribute("message", "Hello world!");
|
||||
return "index";
|
||||
}
|
||||
}
|
||||
|
||||
11
src/main/webapp/WEB-INF/templates/index.jsp
Normal file
11
src/main/webapp/WEB-INF/templates/index.jsp
Normal 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>
|
||||
Reference in New Issue
Block a user