Skip to content

Commit

Permalink
JSTL & Hibernate Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
anantjain6 committed May 9, 2020
1 parent 10eb5db commit fa0c1ca
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 74 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ It is a marketplace where customer can place order and Admin can manage inventor
- Spring Data JPA for Creating Custom Repository
- Spring Boot for Autoconfiguration and Dependency Management
- Spring Security for Authentication & Authorisation
- Hibernate Validator for form data validation
- H2 In-memory Database for Storing data
- Java Mail API to send HTML E-Mail over SMTP
- JSTL

## Requirements
- Java
Expand Down
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down Expand Up @@ -79,6 +80,24 @@
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
</dependencies>

<build>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/me/anant/PMS/PmsApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class PmsApplication {

public static void main(String[] args) {
SpringApplication.run(PmsApplication.class, args);
System.out.println("Server Started");
}

}
32 changes: 20 additions & 12 deletions src/main/java/me/anant/PMS/controller/ProductController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import java.util.List;
import java.util.Optional;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
Expand All @@ -28,16 +32,20 @@ public class ProductController {
public ModelAndView addView() {
ModelAndView modelAndView = new ModelAndView("admin/product/add");
modelAndView.addObject("pcList", categoryService.get());
modelAndView.addObject("command", new Product());
return modelAndView;
}

@PostMapping("/admin/product/add")
public String add(Product product, @RequestParam("productCategory") long category, Model model, final RedirectAttributes redirectAttributes) {
product.setCategory(categoryService.findById(category).get());
public String add(@ModelAttribute("command") @Valid Product product, BindingResult result, Model model, final RedirectAttributes redirectAttributes) {
if(result.hasErrors()) {
model.addAttribute("pcList", categoryService.get());
return "admin/product/add";
}
productService.save(product);
redirectAttributes.addFlashAttribute("msg", "Product added successfully");
redirectAttributes.addFlashAttribute("class", "alert-success");
return "redirect:/admin/product/list";
return "redirect:/admin/product/add";
}

@GetMapping("/admin/product/list")
Expand All @@ -62,21 +70,21 @@ public ModelAndView updateView(long id) {
Optional<Product> optional = productService.findById(id);
Product product = optional.get();
ModelAndView modelAndView = new ModelAndView("admin/product/add");
modelAndView.addObject("product", product);
modelAndView.addObject("command", product);
modelAndView.addObject("pcList", categoryService.get());
return modelAndView;
}

@PostMapping("/admin/product/update")
public ModelAndView updateView(Product product, @RequestParam("productCategory") long category) {
product.setCategory(categoryService.findById(category).get());
public String updateView(@ModelAttribute("command") @Valid Product product, BindingResult result, Model model, final RedirectAttributes redirectAttributes) {
if(result.hasErrors()) {
model.addAttribute("pcList", categoryService.get());
return "admin/product/add";
}
productService.save(product);
ModelAndView modelAndView = new ModelAndView("admin/product/add");
modelAndView.addObject("product", product);
modelAndView.addObject("pcList", categoryService.get());
modelAndView.addObject("msg", "Product Updated successfully.");
modelAndView.addObject("class", "alert-success");
return modelAndView;
redirectAttributes.addFlashAttribute("msg", "Product Updated successfully");
redirectAttributes.addFlashAttribute("class", "alert-success");
return "redirect:/admin/product/list";
}

@GetMapping("/admin/product/report")
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/me/anant/PMS/model/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,29 @@
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
public class Product {
@Id
@GeneratedValue
long productId;


@NotBlank(message="Product name can not be empty")
@Size(min = 3, message="Product name should be of minimum 3 character")
String productName;

@NotNull
@Min(value=0, message="Product price can not be negative value.")
float productPrice;

@NotNull
@Min(value=0, message="Product quantity can not be negative")
@Max(value=50, message="Product quantity can be maximum 50.")
int productQty;

@OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
Expand All @@ -30,6 +44,11 @@ public Product() {
// TODO Auto-generated constructor stub
}

public Product(long productId) {
super();
this.productId = productId;
}

public Product(String productName, float productPrice, int productQty, ProductCategory category) {
super();
this.productName = productName;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/me/anant/PMS/model/ProductCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public ProductCategory() {
// TODO Auto-generated constructor stub
}

public ProductCategory(long id) {
super();
this.id = id;
}

public ProductCategory(String name) {
super();
this.name = name;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/me/anant/PMS/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class User {
public User() {
// TODO Auto-generated constructor stub
}
public User(long id) {
super();
this.id = id;
}
public User(String name, String email, String password, String role) {
super();
this.name = name;
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ INSERT INTO PRODUCT_CATEGORY (id, NAME) VALUES
(2, 'Garment');

INSERT INTO PRODUCT(PRODUCT_ID, PRODUCT_NAME, PRODUCT_PRICE, PRODUCT_QTY, CATEGORY_ID) VALUES
(101, 'Apple', '20.5', 100, 1),
(102, 'Orange', '10.25', 200, 1),
(103, 'Shirt', '220.35', 60, 2),
(104, 'Pant', '410.25', 30, 2);
(101, 'Apple', '20.5', 11, 1),
(102, 'Orange', '10.25', 22, 1),
(103, 'Shirt', '220.35', 33, 2),
(104, 'Pant', '410.25', 44, 2);
85 changes: 29 additions & 56 deletions src/main/webapp/admin/product/add.jsp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="me.anant.PMS.model.ProductCategory"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="me.anant.PMS.model.Product"%>
Expand All @@ -8,78 +10,49 @@
</head>
<body style="background-color: #f8f9fa !important;">
<%@include file="/includes/header.jsp"%>
<main role="main" class="container"> <%@include file="/includes/msg.jsp"%>
<main role="main" class="container">
<%@include file="/includes/msg.jsp"%>
<div class="card">
<c:set var="action" value="/admin/product/add"/>
<c:set var="title" value="Add Product"/>
<c:if test="${command.productId > 0}">
<c:set var="action" value="/admin/product/update"/>
<c:set var="title" value="Update Product"/>
</c:if>
<div class="card-header text-white shadow bg-dark">
<h2 class="float-left"><%
if(request.getAttribute("product") == null) {
out.write("Add Product");
} else {
out.write("Update Product");
}
%></h2>
<h2 class="float-left">${title}</h2>
</div>
<div class="card-body">
<form action="<%
if(request.getAttribute("product") == null) {
out.write("/admin/product/add");
} else {
out.write("/admin/product/update?id="+request.getParameter("id"));
}
%>" method="post">
<% if(request.getAttribute("product") != null) {
out.write("<input type=\"hidden\" value=\""+request.getParameter("id")+"\" name=\"productId\">");
} %>
<form:form action="${action}" method="post">
<form:input type="hidden" path="productId"/>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<div class="form-group">
<label for="productName" class="control-label">Product Name</label> <input type="text" name="productName" id="productName" class="form-control"
required <% if(request.getAttribute("product") != null) {
Product p = (Product) request.getAttribute("product");
out.write("value=\""+p.getProductName()+"\"");
} %>>
<label for="productName" class="control-label">Product Name</label>
<form:input type="text" path="productName" id="productName" cssClass="form-control" required="required"/>
<small class="form-text text-muted"><font color="red"><form:errors path="productName"></form:errors></font></small>
</div>
<div class="form-group">
<label for="productPrice" class="control-label">Product Price</label> <input type="text" name="productPrice" id="productPrice" class="form-control"
required <% if(request.getAttribute("product") != null) {
Product p = (Product) request.getAttribute("product");
out.write("value=\""+p.getProductPrice()+"\"");
} %>>
<label for="productPrice" class="control-label">Product Price</label>
<form:input type="text" path="productPrice" id="productPrice" cssClass="form-control" required="required"/>
<small class="form-text text-muted"><font color="red"><form:errors path="productPrice"></form:errors></font></small>
</div>
<div class="form-group">
<label for="productQty" class="control-label">Product Qty</label> <input type="text" name="productQty" id="productQty" class="form-control"
required <% if(request.getAttribute("product") != null) {
Product p = (Product) request.getAttribute("product");
out.write("value=\""+p.getProductQty()+"\"");
} %>>
<label for="productQty" class="control-label">Product Qty</label>
<form:input type="text" path="productQty" id="productQty" cssClass="form-control" required="required"/>
<small class="form-text text-muted"><font color="red"><form:errors path="productQty"></form:errors></font></small>
</div>
<div class="form-group">
<label for="productCategory" class="control-label">Product Category</label>
<select name="productCategory" id="productCategory" class="form-control">
<%
List<ProductCategory> pcList = (List<ProductCategory>) request.getAttribute("pcList");
for(ProductCategory pc: pcList) {
out.write("<option value=\""+pc.getId()+"\" ");
if(request.getAttribute("product") != null) {
Product p = (Product) request.getAttribute("product");
if(p.getCategory().getId() == pc.getId()) {
out.write("selected");
}
}
out.write(">"+pc.getName()+"</option>");
}
%>
</select>
<form:select path="category.id" id="productCategory" cssClass="form-control">
<c:forEach items="${pcList}" var="category">
<form:option value="${category.id}">${category.name}</form:option>
</c:forEach>
</form:select>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success btn-lg btn-block" value="<%
if(request.getAttribute("product") == null) {
out.write("Add Product");
} else {
out.write("Update Product");
}
%>">
<input type="submit" class="btn btn-success btn-lg btn-block" value="${title}">
</div>
</form>
</form:form>
</div>
</div>
</main>
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/customer/home.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<td>
<select class="form-control" name="<%=p.getProductId()%>">
<%
for(int i=0; i<=p.getProductQty(); i++) {
for(int i=1; i<=p.getProductQty(); i++) {
out.write("<option value=\""+i+"\">"+i+"</option>");
}
%>
Expand Down

0 comments on commit fa0c1ca

Please sign in to comment.