Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/sonarlint-state.xml
.idea/sonarlint
/.idea/sonarlint/

# Gradle
.idea/**/gradle.xml
Expand All @@ -44,3 +44,4 @@ target/
build/
logs/
data/

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@


plugins {
id 'java'
id 'war'
id "org.gretty" version "3.0.3"
}

group 'ru.dimon.spring'
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 = '/'
}

Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 0 additions & 2 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions src/main/java/lessontwo/mvc/AppConfig.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
24 changes: 24 additions & 0 deletions src/main/java/lessontwo/mvc/Launcher.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
36 changes: 36 additions & 0 deletions src/main/java/lessontwo/mvc/WebInitializer.java
Original file line number Diff line number Diff line change
@@ -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};
}

}
14 changes: 14 additions & 0 deletions src/main/java/lessontwo/mvc/controllers/MainController.java
Original file line number Diff line number Diff line change
@@ -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";
}

}
55 changes: 55 additions & 0 deletions src/main/java/lessontwo/mvc/controllers/ProductsController.java
Original file line number Diff line number Diff line change
@@ -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<Product> 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<Product> 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/";
}


}
55 changes: 55 additions & 0 deletions src/main/java/lessontwo/mvc/model/Product.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
52 changes: 52 additions & 0 deletions src/main/java/lessontwo/mvc/repositories/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -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<Product> 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<Product> 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;
}
}
}
Loading