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
4 changes: 3 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,5 @@ target/
build/
logs/
data/

.idea/dataSources.xml
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.

3 changes: 3 additions & 0 deletions .idea/misc.xml

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

29 changes: 26 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@


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'

compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.4.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.22'
// https://mvnrepository.com/artifact/com.h2database/h2
compile group: 'com.h2database', name: 'h2', version: '1.4.200'

}

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/lessonthree/hibernate/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package lessonthree.hibernate;

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("lessonthree.hibernate")
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/lessonthree/hibernate/Launcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lessonthree.hibernate;

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/lessonthree/hibernate/WebInitializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package lessonthree.hibernate;

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package lessonthree.hibernate.controllers;

import lessonthree.hibernate.model.Customer;
import lessonthree.hibernate.services.CustomerService;
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("customers")
public class CustomersController {
@Autowired
CustomerService customerService;

@RequestMapping(value = "", method = RequestMethod.GET)
public String showAllCustomers(Model model) {
List<Customer> customerList = customerService.getAllCustomers();
model.addAttribute("customers", customerList);
return "customers/all_customers";
}

@RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
public String showInfoCustomerForm(@PathVariable long id, Model model) {
model.addAttribute("customer", customerService.get(id));
return "customers/info_customer";
}

@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
public String showEditCustomerForm(@PathVariable long id, Model model) {
model.addAttribute("customer", customerService.get(id));
return "customers/edit_customer_form";
}

@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String editCustomer(@ModelAttribute Customer customer) {
customerService.update(customer);
return "redirect:/customers/";
}

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String showAddCustomerForm(Model model) {
model.addAttribute("customer", new Customer());
return "customers/add_customer_form";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addCustomer(@ModelAttribute Customer customer) {
customerService.add(customer);
return "redirect:/customers/";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package lessonthree.hibernate.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";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package lessonthree.hibernate.controllers;

import lessonthree.hibernate.model.Order;
import lessonthree.hibernate.services.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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("orders")
public class OrdersController {
@Autowired
OrdersService ordersService;

@RequestMapping(value = "", method = RequestMethod.GET)
public String showAllOrder(Model model) {
List<Order> orderList = ordersService.getAllOrders();
model.addAttribute("orders", orderList);
return "orders/all_orders";
}

@RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
public String showInfoOrderForm(@PathVariable long id, Model model) {
model.addAttribute("order", ordersService.get(id));
return "orders/info_order";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package lessonthree.hibernate.controllers;

import lessonthree.hibernate.model.Product;
import lessonthree.hibernate.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 "products/all_products";
}

@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
public String showEditProductForm(@PathVariable long id, Model model) {
model.addAttribute("product", productsService.get(id));
return "products/edit_product_form";
}

@RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
public String showInfoProductForm(@PathVariable long id, Model model) {
model.addAttribute("product", productsService.get(id));
return "products/info_product";
}

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

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addProduct(@ModelAttribute Product product) {
productsService.add(product);
return "redirect:/products/";
}




}
Loading