Skip to content

Commit 56f9fd5

Browse files
Added Product Listing
1 parent ab33273 commit 56f9fd5

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package guru.springframework.controllers;
2+
3+
import guru.springframework.services.ProductService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
9+
/**
10+
* Created by jt on 11/6/15.
11+
*/
12+
@Controller
13+
public class ProductController {
14+
15+
private ProductService productService;
16+
17+
@Autowired
18+
public void setProductService(ProductService productService) {
19+
this.productService = productService;
20+
}
21+
22+
@RequestMapping("/products")
23+
public String listProducts(Model model){
24+
25+
model.addAttribute("products", productService.listAllProducts());
26+
27+
return "products";
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package guru.springframework.domain;
2+
3+
import java.math.BigDecimal;
4+
5+
/**
6+
* Created by jt on 11/6/15.
7+
*/
8+
public class Product {
9+
private Integer id;
10+
private String description;
11+
private BigDecimal price;
12+
private String imageUrl;
13+
14+
public Integer getId() {
15+
return id;
16+
}
17+
18+
public void setId(Integer id) {
19+
this.id = id;
20+
}
21+
22+
public String getDescription() {
23+
return description;
24+
}
25+
26+
public void setDescription(String description) {
27+
this.description = description;
28+
}
29+
30+
public BigDecimal getPrice() {
31+
return price;
32+
}
33+
34+
public void setPrice(BigDecimal price) {
35+
this.price = price;
36+
}
37+
38+
public String getImageUrl() {
39+
return imageUrl;
40+
}
41+
42+
public void setImageUrl(String imageUrl) {
43+
this.imageUrl = imageUrl;
44+
}
45+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package guru.springframework.services;
2+
3+
import guru.springframework.domain.Product;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Created by jt on 11/6/15.
9+
*/
10+
public interface ProductService {
11+
12+
List<Product> listAllProducts();
13+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package guru.springframework.services;
2+
3+
import guru.springframework.domain.Product;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.math.BigDecimal;
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
/**
13+
* Created by jt on 11/6/15.
14+
*/
15+
@Service
16+
public class ProductServiceImpl implements ProductService {
17+
18+
private Map<Integer,Product> products;
19+
20+
public ProductServiceImpl() {
21+
loadProducts();
22+
}
23+
24+
@Override
25+
public List<Product> listAllProducts() {
26+
return new ArrayList<>(products.values());
27+
}
28+
29+
private void loadProducts(){
30+
products = new HashMap<>();
31+
32+
Product product1 = new Product();
33+
product1.setId(1);
34+
product1.setDescription("Product 1");
35+
product1.setPrice(new BigDecimal("12.99"));
36+
product1.setImageUrl("http://example.com/product1");
37+
38+
products.put(1, product1);
39+
40+
Product product2 = new Product();
41+
product2.setId(2);
42+
product2.setDescription("Product 2");
43+
product2.setPrice(new BigDecimal("14.99"));
44+
product2.setImageUrl("http://example.com/product2");
45+
46+
products.put(2, product2);
47+
48+
Product product3 = new Product();
49+
product3.setId(3);
50+
product3.setDescription("Product 3");
51+
product3.setPrice(new BigDecimal("34.99"));
52+
product3.setImageUrl("http://example.com/product3");
53+
54+
products.put(3, product3);
55+
56+
Product product4 = new Product();
57+
product4.setId(4);
58+
product4.setDescription("Product 4");
59+
product4.setPrice(new BigDecimal("44.99"));
60+
product4.setImageUrl("http://example.com/product4");
61+
62+
products.put(4, product4);
63+
64+
Product product5 = new Product();
65+
product5.setId(5);
66+
product5.setDescription("Product 2");
67+
product5.setPrice(new BigDecimal("25.99"));
68+
product5.setImageUrl("http://example.com/product5");
69+
70+
products.put(5, product5);
71+
}
72+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Spring Core Online Tutorial - List Products</title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
6+
7+
<link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
8+
th:href="@{/webjars/bootstrap/3.3.5/css/bootstrap.min.css}"
9+
rel="stylesheet" media="screen"/>
10+
11+
<script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js"
12+
th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>
13+
14+
<link href="../static/css/spring-core.css"
15+
th:href="@{css/spring-core.css}" rel="stylesheet" media="screen"/>
16+
</head>
17+
<body>
18+
<div class="container">
19+
<div th:if="${not #lists.isEmpty(products)}">
20+
<h2>Product List</h2>
21+
<table class="table table-striped">
22+
<tr>
23+
<th>Id</th>
24+
<th>Description</th>
25+
<th>Price</th>
26+
<th>Image URL</th>
27+
</tr>
28+
<tr th:each="product : ${products}">
29+
<td th:text="${product.id}"></td>
30+
<td th:text="${product.description}"></td>
31+
<td th:text="${product.price}"></td>
32+
<td th:text="${product.imageUrl}"></td>
33+
</tr>
34+
</table>
35+
</div>
36+
</div>
37+
38+
</body>
39+
</html>

0 commit comments

Comments
 (0)