Skip to content

Part3 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: part2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4ed4d5d
creating branch
jt1088 Jun 16, 2015
02a1d4b
added tutorial files
jt1088 Jun 25, 2015
6962e2d
added templates dir & temp file for git
springframeworkguru Nov 22, 2015
8dba4a5
Added Post on Configuring Spring Boot for PostgreSQL
ximanta Dec 27, 2015
9106bc8
Merge pull request #3 from ximanta/master
springframeworkguru Dec 30, 2015
0e44720
Updated SpringBoot reference to 14.2 and @EntityScan package
Dec 5, 2016
90f6c9b
Updated ServletRegistrationBean to org.springframework.boot.web.servl…
Dec 5, 2016
16bf006
Updated @SpringApplicationConfiguration to @SpringBootTest
Dec 5, 2016
e6180d4
Merge pull request #10 from ximanta/master
springframeworkguru Dec 5, 2016
06a489c
Updated pom.xml to use SpringBoot 1.4.2.RELEASE
Dec 6, 2016
0480344
Updated WebConfiguration to use the new org.springframework.boot.web.…
Dec 6, 2016
cf0526f
Merge pull request #12 from ximanta/part3
springframeworkguru Dec 6, 2016
4c69c94
Updated @SpringApplicationConfiguration to @SpringBootTest.
Dec 7, 2016
b01fcc5
Merge pull request #14 from ximanta/part3
springframeworkguru Dec 7, 2016
7907467
Updated @EnityScan in favor of org.springframework.boot.autoconfigure…
Dec 7, 2016
cc2472d
Merge pull request #15 from ximanta/part3
springframeworkguru Dec 7, 2016
6f8c95c
removing postgresql (oops), updating to latest Spring Boot Version
springframeworkguru May 4, 2017
52788ff
Updated to Spring Boot 2.0.0.RELEASE and fixed JPA breaks.
springframeworkguru Mar 23, 2018
afa695b
Updated to Spring Boot 2.2.2.RELEASE in Java 11
springframeworkguru May 11, 2020
be013f1
Updated to Spring Boot 2.2.2.RELEASE and Java 11
springframeworkguru May 11, 2020
69a322a
Merge branch 'master' into part2
omariit Jun 1, 2021
b4d3a5d
Merge branch 'part2' into part3
omariit Jun 1, 2021
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# Spring Boot Web Application
This repository has the project files for a tutorial series on Spring Boot available from by website at [Spring Framework Guru](https://springframework.guru)

##Part 3
This repository has the project files for a tutorial series on Spring Boot available from by website at [Spring Framework Guru](https://springframework.guru)

This repository has the project files for a tutorial series on Spring Boot available from by website at [Spring Framework Guru](https://springframework.guru)

## Checkout the full tutorial here!
[Spring Boot - making Spring Fun again!](https://springframework.guru/spring-boot-web-application-part-1-spring-initializr/)
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>

</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/guru/springframework/bootstrap/ProductLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package guru.springframework.bootstrap;

import guru.springframework.domain.Product;
import guru.springframework.repositories.ProductRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class ProductLoader implements ApplicationListener<ContextRefreshedEvent> {

private ProductRepository productRepository;

private Logger log = LoggerFactory.getLogger(ProductLoader.class);


@Autowired
public void setProductRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {

Product shirt = new Product();
shirt.setDescription("Spring Framework Guru Shirt");
shirt.setPrice(new BigDecimal("18.95"));
shirt.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_shirt-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg");
shirt.setProductId("235268845711068308");
productRepository.save(shirt);

log.info("Saved Shirt - id: " + shirt.getId());

Product mug = new Product();
mug.setDescription("Spring Framework Guru Mug");
mug.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_coffee_mug-r11e7694903c348e1a667dfd2f1474d95_x7j54_8byvr_512.jpg");
mug.setProductId("168639393495335947");

productRepository.save(mug);

log.info("Saved Mug - id:" + mug.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests().antMatchers("/").permitAll();

httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
.authorizeRequests().antMatchers("/console/**").permitAll();

httpSecurity.csrf().disable();
httpSecurity.headers().frameOptions().disable();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package guru.springframework.configuration;

import org.h2.server.web.WebServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfiguration {
@Bean
ServletRegistrationBean h2servletRegistration(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
registrationBean.addUrlMappings("/console/*");
return registrationBean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package guru.springframework.controllers;

import guru.springframework.domain.Product;
import guru.springframework.services.ProductService;
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;

@Controller
public class ProductController {

private ProductService productService;

@Autowired
public void setProductService(ProductService productService) {
this.productService = productService;
}

@RequestMapping(value = "/products", method = RequestMethod.GET)
public String list(Model model){
model.addAttribute("products", productService.listAllProducts());
System.out.println("Returning rpoducts:");
return "products";
}

@RequestMapping("product/{id}")
public String showProduct(@PathVariable Integer id, Model model){
model.addAttribute("product", productService.getProductById(id));
return "productshow";
}

@RequestMapping("product/edit/{id}")
public String edit(@PathVariable Integer id, Model model){
model.addAttribute("product", productService.getProductById(id));
return "productform";
}

@RequestMapping("product/new")
public String newProduct(Model model){
model.addAttribute("product", new Product());
return "productform";
}

@RequestMapping(value = "product", method = RequestMethod.POST)
public String saveProduct(Product product){

productService.saveProduct(product);

return "redirect:/product/" + product.getId();
}

}
67 changes: 67 additions & 0 deletions src/main/java/guru/springframework/domain/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package guru.springframework.domain;

import javax.persistence.*;
import java.math.BigDecimal;

@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Version
private Integer version;

private String productId;
private String description;
private String imageUrl;
private BigDecimal price;

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public String getImageUrl() {
return imageUrl;
}

public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}

public BigDecimal getPrice() {
return price;
}

public void setPrice(BigDecimal price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package guru.springframework.repositories;

import guru.springframework.domain.Product;
import org.springframework.data.repository.CrudRepository;

public interface ProductRepository extends CrudRepository<Product, Integer>{
}
12 changes: 12 additions & 0 deletions src/main/java/guru/springframework/services/ProductService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package guru.springframework.services;


import guru.springframework.domain.Product;

public interface ProductService {
Iterable<Product> listAllProducts();

Product getProductById(Integer id);

Product saveProduct(Product product);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package guru.springframework.services;

import guru.springframework.domain.Product;
import guru.springframework.repositories.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {
private ProductRepository productRepository;

@Autowired
public void setProductRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}

@Override
public Iterable<Product> listAllProducts() {
return productRepository.findAll();
}

@Override
public Product getProductById(Integer id) {
return productRepository.findById(id).orElse(null);
}

@Override
public Product saveProduct(Product product) {
return productRepository.save(product);
}
}
5 changes: 5 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#logging.level.org.h2.server: DEBUG

spring.jpa.hibernate.ddl-auto=create-drop

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions src/main/resources/templates/fragments/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
th:href="@{/webjars/bootstrap/3.3.4/css/bootstrap.min.css}"
rel="stylesheet" media="screen"/>

<script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js"
th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>

<link href="../../static/css/guru.css"
th:href="@{css/guru.css}" rel="stylesheet" media="screen"/>
</head>
<body>

<div class="container">
<div th:fragment="header">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#" th:href="@{/}">Home</a>
<ul class="nav navbar-nav">
<li><a href="#" th:href="@{/products}">Products</a></li>
<li><a href="#" th:href="@{/product/new}">Create Product</a></li>
</ul>

</div>
</div>
</nav>

<div class="jumbotron">
<div class="row text-center">
<div class="">
<h2>Spring Framework Guru</h2>

<h3>Spring Boot Web App</h3>
</div>
</div>
<div class="row text-center">
<img src="../../static/images/NewBannerBOOTS_2.png" width="400"
th:src="@{/images/NewBannerBOOTS_2.png}"/>
</div>
</div>
</div>
</div>
</body>
</html>
18 changes: 18 additions & 0 deletions src/main/resources/templates/fragments/headerinc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en" th:fragment="head">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
th:href="@{/webjars/bootstrap/3.3.4/css/bootstrap.min.css}"
rel="stylesheet" media="screen" />

<script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js"
th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>

<link href="../static/css/guru.css"
th:href="@{/css/guru.css}" rel="stylesheet" media="screen"/>
</head>
<body>

</body>
</html>
3 changes: 3 additions & 0 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ <h2>Fellow Spring Framework Gurus!!!</h2>
</div>
</div>

<div class="container">
<!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->
</div>
</body>
</html>
Loading