NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io



























==============================================================================
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.18</version>
</dependency>

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

<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>


<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.7.Final</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
==============================================================================
spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">

<tx:annotation-driven/>

<context:component-scan base-package="productcrudapp"></context:component-scan>

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
name="viewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp"></property>
</bean>



<!-- data source -->

<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
name="ds">

<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/productcrud" />
<property name="username" value="root" />
<property name="password" value="root" />

</bean>


<!-- LocalSessionFactoryBean -->



<bean
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
name="factory">

<!-- data source -->
<property name="dataSource" ref="ds"></property>

<!-- hibernate properties -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>

<!-- annotated classes -->
<property name="annotatedClasses">
<list>
<value>productcrudapp.model.Product</value>
</list>
</property>

</bean>

<bean class="org.springframework.orm.hibernate5.HibernateTemplate"
name="hibernateTemplate">
<property name="sessionFactory" ref="factory"></property>

</bean>


<bean
class="org.springframework.orm.hibernate5.HibernateTransactionManager"
name="transactionManager">
<property name="sessionFactory" ref="factory"></property>
</bean>

</beans>
==============================================================================
Product.java

@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String description;
private long price;


==============================================================================
ProductDao.java

package productcrudapp.dao;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Component;

import productcrudapp.model.Product;

@Component
public class ProductDao {

@Autowired
private HibernateTemplate hibernateTemplate;

// create
@Transactional
public void createProduct(Product product) {

this.hibernateTemplate.saveOrUpdate(product);

}

// get all products
public List<Product> getProducts() {
List<Product> products = this.hibernateTemplate.loadAll(Product.class);
return products;
}

// delete the single product
@Transactional
public void deleteProduct(int pid) {
Product p = this.hibernateTemplate.get(Product.class, pid);
this.hibernateTemplate.delete(p);
}

// get the single product
public Product getProduct(int pid) {
return this.hibernateTemplate.get(Product.class, pid);
}
}


==============================================================================
MainController.java

package productcrudapp.controller;

import java.util.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

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 org.springframework.web.servlet.view.RedirectView;

import productcrudapp.dao.ProductDao;
import productcrudapp.model.Product;

@Controller
public class MainController {

@Autowired
private ProductDao productDao;

@RequestMapping("/")
public String home(Model m)
{
List<Product> products=productDao.getProducts();
m.addAttribute("product",products);
return "index";
}

@RequestMapping("/add-product")
public String addProduct(Model m )
{
m.addAttribute("title","Add Product");
return "add_product_form";
}

@RequestMapping(value="/handle-product",method = RequestMethod.POST)
public RedirectView handleProduct(@ModelAttribute Product product,HttpServletRequest request) {
System.out.println(product);
productDao.createProduct(product);
RedirectView redirectView =new RedirectView();
redirectView.setUrl(request.getContextPath()+"/");
return redirectView;

}

@RequestMapping("/delete/{productId}")
public RedirectView deleteProduct(@PathVariable("productId") int productId, HttpServletRequest request) {
this.productDao.deleteProduct(productId);
RedirectView redirectView =new RedirectView();
redirectView.setUrl(request.getContextPath()+"/");
return redirectView;
}
@RequestMapping("/update/{productId}")
public String updateForm(@PathVariable("productId") int pid,Model model)
{
Product product=this.productDao.getProduct(pid);
model.addAttribute("product",product);
return "update_form";
}

}

==============================================================================
index.jsp

<html>
<head>
<%@include file="./base.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
</head>
<body>
<center><h2>Welcome to Employee Information System</h2></center>
<table class="table table-dark">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Employee Name</th>
<th scope="col">Employee Description</th>
<th scope="col">Employee Salary</th>
<th scope="col">Action</th>

</tr>
</thead>
<tbody>

<c:forEach items="${product}" var="p">
<tr>
<th scope="row">Employee ${p.id }</th>
<td>${p.name }</td>
<td>${p.description }</td>
<td class="font-weight-bold">&#x20B9;${p.price}</td>
<td>
<a href="delete/${p.id }"><i class="fas fa-trash"></i></a>
<a href="update/${p.id }"><i class="fas fa-pen-nib"></i></a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="container text">
<a href="add-product" class="btn btn-outline-success">Add Employee</a>
</div>
</body>
</html>

==============================================================================
add_product_form.java

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page isELIgnored = "false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<%@include file="./base.jsp" %>
</head>
<body>
<div class="card card-outline-secondary">
<div class="card-header">
<h3 class="mb-0">Employee Information</h3>
</div>
<div class="card-body">
<form action="handle-product" method="post">

<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Employee name</label>
<div class="col-lg-9">
<input class="form-control" type="text" name="name" id="name">
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Employee description</label>
<div class="col-lg-9">
<input class="form-control" type="text" name="description" id="description">
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Employee Salary</label>
<div class="col-lg-9">
<input class="form-control" type="text" name="price" id="price">
</div>
</div>






<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label"></label>
<div class="col-lg-9">
<a href="${pageContext.request.contextPath}/" class="btn btn-secondary" >Back</a>
<button class="btn btn-primary" type="submit" >Add</button>
</div>
</div>
</form>
</div>
</div><!-- /form user info -->
</body>
</html>

==============================================================================
update_form.java

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<%@include file="./base.jsp" %>
</head>
<body>
<div class="card card-outline-secondary">
<div class="card-header">
<h3 class="mb-0">Update Employee Information</h3>
</div>
<div class="card-body">
<form action="${pageContext.request.contextPath }/handle-product" method="post">
<input type="hidden" value="${product.id }" name="id" />
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Employee name</label>
<div class="col-lg-9">
<input class="form-control" type="text" name="name" id="name" value="${product.name }">
</div>
</div>
. <div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Employee description</label>
<div class="col-lg-9">
<input class="form-control" type="text" name="description" id="description" value="${product.description }">
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Employee Salary</label>
<div class="col-lg-9">
<input class="form-control" type="text" name="price" id="price" value="${product.price }">
</div>
</div>






<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label"></label>
<div class="col-lg-9">
<a href="${pageContext.request.contextPath}/" class="btn btn-secondary" >Back</a>
<button class="btn btn-primary" type="submit" >Update</button>
</div>
</div>
</form>
</div>
</div><!-- /form user info -->
</body>
</html>
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.