diff --git a/.gitignore b/.gitignore
index a285bb3..17bc03c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,7 +20,7 @@
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/sonarlint-state.xml
-.idea/sonarlint
+/.idea/sonarlint/
# Gradle
.idea/**/gradle.xml
@@ -44,3 +44,4 @@ target/
build/
logs/
data/
+
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
index fdc392f..b3e9cbd 100644
--- a/.idea/jarRepositories.xml
+++ b/.idea/jarRepositories.xml
@@ -16,5 +16,10 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/build.gradle b/build.gradle
index fa0ea28..65f3f2b 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,7 +1,6 @@
-
-
plugins {
- id 'java'
+ id 'war'
+ id "org.gretty" version "3.0.3"
}
group 'ru.dimon.spring'
@@ -9,10 +8,28 @@ version '1.0-SNAPSHOT'
repositories {
mavenCentral()
+ jcenter()
+
}
+def springVersion = '5.2.10.RELEASE'
+def jettyVersion = '9.2.26.v20180806'
+
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
+
+ compile group: 'org.springframework', name: 'spring-webmvc', version: springVersion
+ compile group: 'org.eclipse.jetty', name: 'jetty-server', version: jettyVersion
+ compile group: 'org.eclipse.jetty', name: 'jetty-webapp', version: jettyVersion
+ compile group: 'org.thymeleaf', name: 'thymeleaf-spring5', version: '3.0.10.RELEASE'
+ compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.6'
+ compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.3'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'
}
+
+gretty {
+ httpPort = 8080
+ contextPath = '/'
+}
+
diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
index 62d4c05..490fda8 100644
Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/gradlew b/gradlew
index fbd7c51..2fe81a7 100644
--- a/gradlew
+++ b/gradlew
@@ -82,7 +82,6 @@ esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
-
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
@@ -130,7 +129,6 @@ fi
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
-
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
diff --git a/gradlew.bat b/gradlew.bat
index 5093609..62bd9b9 100644
--- a/gradlew.bat
+++ b/gradlew.bat
@@ -84,7 +84,6 @@ set CMD_LINE_ARGS=%*
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
-
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
diff --git a/src/main/java/lessontwo/mvc/AppConfig.java b/src/main/java/lessontwo/mvc/AppConfig.java
new file mode 100644
index 0000000..de3fed8
--- /dev/null
+++ b/src/main/java/lessontwo/mvc/AppConfig.java
@@ -0,0 +1,46 @@
+package lessontwo.mvc;
+
+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.EnableWebMvc;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import org.thymeleaf.spring5.SpringTemplateEngine;
+import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
+import org.thymeleaf.spring5.view.ThymeleafViewResolver;
+
+@EnableWebMvc
+@Configuration
+@ComponentScan("lessontwo.mvc")
+public class AppConfig implements WebMvcConfigurer {
+ @Override
+ public void addResourceHandlers(ResourceHandlerRegistry registry) {
+ registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
+ }
+
+ @Bean
+ public SpringResourceTemplateResolver templateResolver() {
+ SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
+ templateResolver.setPrefix("/WEB-INF/templates/");
+ templateResolver.setSuffix(".html");
+ return templateResolver;
+ }
+
+ @Bean
+ public SpringTemplateEngine templateEngine() {
+ SpringTemplateEngine templateEngine = new SpringTemplateEngine();
+ templateEngine.setTemplateResolver(templateResolver());
+ return templateEngine;
+ }
+
+ @Bean
+ public ThymeleafViewResolver thymeleafViewResolver() {
+ ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
+ thymeleafViewResolver.setTemplateEngine(templateEngine());
+ thymeleafViewResolver.setCharacterEncoding("UTF-8");
+ return thymeleafViewResolver;
+ }
+
+
+}
diff --git a/src/main/java/lessontwo/mvc/Launcher.java b/src/main/java/lessontwo/mvc/Launcher.java
new file mode 100644
index 0000000..13d5b2f
--- /dev/null
+++ b/src/main/java/lessontwo/mvc/Launcher.java
@@ -0,0 +1,24 @@
+package lessontwo.mvc;
+
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.webapp.WebAppContext;
+
+import java.net.URL;
+import java.security.ProtectionDomain;
+
+public class Launcher {
+ public static void main(String[] args) throws Exception {
+ Server server = new Server(8189);
+
+ ProtectionDomain domain = Launcher.class.getProtectionDomain();
+ URL location = domain.getCodeSource().getLocation();
+
+ WebAppContext webAppContext = new WebAppContext();
+ webAppContext.setContextPath("/");
+ webAppContext.setWar(location.toExternalForm());
+
+ server.setHandler(webAppContext);
+ server.start();
+ server.join();
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/lessontwo/mvc/WebInitializer.java b/src/main/java/lessontwo/mvc/WebInitializer.java
new file mode 100644
index 0000000..5de6a27
--- /dev/null
+++ b/src/main/java/lessontwo/mvc/WebInitializer.java
@@ -0,0 +1,36 @@
+package lessontwo.mvc;
+
+import javax.servlet.Filter;
+import org.springframework.web.filter.CharacterEncodingFilter;
+import org.springframework.web.filter.HiddenHttpMethodFilter;
+import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
+
+public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
+ @Override
+ protected Class>[] getRootConfigClasses() {
+ return null;
+ }
+
+ @Override
+ protected Class>[] getServletConfigClasses() {
+ return new Class[]{AppConfig.class};
+ }
+
+ protected String[] getServletMappings() {
+ return new String[]{"/"};
+ }
+
+ @Override
+ protected Filter[] getServletFilters() {
+ // Создание фильтра кодировки, который позволит работать с русскими
+ // символами
+ CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
+ characterEncodingFilter.setEncoding("UTF-8");
+ characterEncodingFilter.setForceEncoding(true);
+ // Создание фильтра, который добавляет поддержку HTTP-методов (например
+ // таких, как PUT), необходимых для REST API
+ HiddenHttpMethodFilter httpMethodFilter = new HiddenHttpMethodFilter();
+ return new Filter[]{characterEncodingFilter, httpMethodFilter};
+ }
+
+}
diff --git a/src/main/java/lessontwo/mvc/controllers/MainController.java b/src/main/java/lessontwo/mvc/controllers/MainController.java
new file mode 100644
index 0000000..fc9292d
--- /dev/null
+++ b/src/main/java/lessontwo/mvc/controllers/MainController.java
@@ -0,0 +1,14 @@
+package lessontwo.mvc.controllers;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+
+@Controller
+public class MainController {
+
+ @RequestMapping(value = "/", method = RequestMethod.GET)
+ public String showHomePage() {
+ return "index";
+ }
+
+}
diff --git a/src/main/java/lessontwo/mvc/controllers/ProductsController.java b/src/main/java/lessontwo/mvc/controllers/ProductsController.java
new file mode 100644
index 0000000..5a09d99
--- /dev/null
+++ b/src/main/java/lessontwo/mvc/controllers/ProductsController.java
@@ -0,0 +1,55 @@
+package lessontwo.mvc.controllers;
+
+import lessontwo.mvc.model.Product;
+import lessontwo.mvc.services.ProductsService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import java.util.List;
+
+@Controller
+@RequestMapping("products")
+public class ProductsController {
+
+ @Autowired
+ ProductsService productsService;
+
+ @RequestMapping(value = "", method = RequestMethod.GET)
+ public String showAllProducts(Model model) {
+ List productList = productsService.getAllProducts();
+ model.addAttribute("products", productList);
+ return "all_products";
+ }
+
+ @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
+ public String showEditProductForm(@PathVariable long id, Model model) {
+ List productList = productsService.getAllProducts();
+ model.addAttribute("product", productsService.get(id));
+ return "edit_product_form";
+ }
+
+ @RequestMapping(value = "/edit", method = RequestMethod.POST)
+ public String editProduct(@ModelAttribute Product product) {
+ productsService.update(product);
+ return "redirect:/products/";
+ }
+
+ @RequestMapping(value = "/add", method = RequestMethod.GET)
+ public String showAddProductForm(Model model) {
+ model.addAttribute("product", new Product());
+ return "add_product_form";
+ }
+
+ @RequestMapping(value = "/add", method = RequestMethod.POST)
+ public String addProduct(@ModelAttribute Product product) {
+ productsService.add(product);
+ return "redirect:/products/";
+ }
+
+
+}
diff --git a/src/main/java/lessontwo/mvc/model/Product.java b/src/main/java/lessontwo/mvc/model/Product.java
new file mode 100644
index 0000000..f2ab18a
--- /dev/null
+++ b/src/main/java/lessontwo/mvc/model/Product.java
@@ -0,0 +1,55 @@
+package lessontwo.mvc.model;
+
+import java.util.Objects;
+
+public class Product {
+ private Long id;
+ private String title;
+ private float cost;
+
+ public Product() {
+
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public float getCost() {
+ return cost;
+ }
+
+ public void setCost(float cost) {
+ this.cost = cost;
+ }
+
+ public Product(Long id, String title, float cost) {
+ this.id = id;
+ this.title = title;
+ this.cost = cost;
+ }
+
+ @Override
+ public boolean equals(Object o) { if ( this == o ) return true;
+ if ( o == null || getClass() != o.getClass() ) return false;
+ Product product = (Product) o;
+ return Objects.equals(id, product.id);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(id);
+ }
+}
diff --git a/src/main/java/lessontwo/mvc/repositories/ProductRepository.java b/src/main/java/lessontwo/mvc/repositories/ProductRepository.java
new file mode 100644
index 0000000..188bba8
--- /dev/null
+++ b/src/main/java/lessontwo/mvc/repositories/ProductRepository.java
@@ -0,0 +1,52 @@
+package lessontwo.mvc.repositories;
+
+import lessontwo.mvc.model.Product;
+import org.springframework.stereotype.Repository;
+import javax.annotation.PostConstruct;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+@Repository
+public class ProductRepository {
+ private final List products = new ArrayList<>();
+ private Long maxId;
+ @PostConstruct
+ public void init() {
+ products.add(new Product(1L, "product 1", 1.0f));
+ products.add(new Product(2L, "product 2", 1.0f));
+ products.add(new Product(3L, "product 3", 1.0f));
+ products.add(new Product(4L, "product 4", 1.0f));
+ products.add(new Product(5L, "product 5", 1.0f));
+ maxId = 5L;
+ }
+
+ public List getAllProducts() {
+ return Collections.unmodifiableList(products);
+ }
+
+ public Product get(long id) {
+ return products.stream().filter(p -> p.getId() == id).findFirst().orElse(null);
+ }
+
+ public boolean add(Product product) {
+ Long id = product.getId();
+ if (id == null) {
+ maxId ++;
+ product.setId(maxId);
+ return products.add(product);
+ }else{
+ return update(product);
+ }
+ }
+
+ public boolean update(Product product) {
+ int index = products.indexOf(product);
+ if(index == -1 ) {
+ return add(product);
+ }else{
+ products.set(index, product);
+ return true;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/lessontwo/mvc/services/ProductsService.java b/src/main/java/lessontwo/mvc/services/ProductsService.java
new file mode 100644
index 0000000..d6066a2
--- /dev/null
+++ b/src/main/java/lessontwo/mvc/services/ProductsService.java
@@ -0,0 +1,33 @@
+package lessontwo.mvc.services;
+
+import lessontwo.mvc.model.Product;
+import lessontwo.mvc.repositories.ProductRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Collections;
+import java.util.List;
+
+@Service
+public class ProductsService {
+
+ @Autowired
+ private ProductRepository productRepository;
+
+ public List getAllProducts() {
+ return productRepository.getAllProducts();
+ }
+
+ public Product get(long id) {
+ return productRepository.get(id);
+ }
+
+ public boolean add(Product product) {
+ return productRepository.add(product);
+ }
+
+ public boolean update(Product product) {
+ return productRepository.update(product);
+ }
+
+}
diff --git a/src/main/webapp/WEB-INF/templates/add_product_form.html b/src/main/webapp/WEB-INF/templates/add_product_form.html
new file mode 100644
index 0000000..1e23789
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/add_product_form.html
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ Add new product
+
+
+
+
+ to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/all_products.html b/src/main/webapp/WEB-INF/templates/all_products.html
new file mode 100644
index 0000000..e5d3b18
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/all_products.html
@@ -0,0 +1,34 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ Products list:
+
+
+
+ Id
+ Title
+ Cost
+ ...
+
+
+
+
+
+
+
+ edit
+
+
+
+ add new product
+
+ to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/edit_product_form.html b/src/main/webapp/WEB-INF/templates/edit_product_form.html
new file mode 100644
index 0000000..18d2913
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/edit_product_form.html
@@ -0,0 +1,24 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ Edit product
+
+
+ to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/index.html b/src/main/webapp/WEB-INF/templates/index.html
new file mode 100644
index 0000000..b7e4f5b
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/index.html
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ WEB: Spring MVC GeekApp
+ Site map
+ Products list
+
+ Add new product
+
+
+
\ No newline at end of file