NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

POM.XML
---------
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.operation</groupId>
<artifactId>prodictcrudapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>prodictcrudapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.18</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.8.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<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>


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
<finalName>prodictcrudapp</finalName>
</build>

</project>
==========================================================================================
WEB.XML
----------
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
=======================================================================================
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="prodictcrudapp"></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> -->
<value>prodictcrudapp.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
--------------
package prodictcrudapp.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String description;
private long price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public long getPrice() {
return price;
}
public void setPrice(long price) {
this.price = price;
}
public Product(int id, String name, String description, long price) {
super();
this.id = id;
this.name = name;
this.description = description;
this.price = price;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", description=" + description + ", price=" + price + "]";
}



}
===========================================================================================
ProductDao.java
---------------------
package prodictcrudapp.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 prodictcrudapp.model.Product;

@Component
public class ProductDao {

@Autowired
private HibernateTemplate hibernateTemplate;


//create
@Transactional
public void createProduct(Product product)
{
this.hibernateTemplate.saveOrUpdate(product);
}

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

@Transactional
public void deleteProduct(int pid)
{
Product p=this.hibernateTemplate.load(Product.class, pid);
this.hibernateTemplate.delete(p);
}

public Product getProduct(int pid)
{
return this.hibernateTemplate.get(Product.class, pid);

}

}
==============================================================================================
base.jsp
------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page isELIgnored="false"%>

<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"
integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g=="
crossorigin="anonymous" referrerpolicy="no-referrer" />

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>

<title><c:out value="${title }">Product Crud APP</c:out></title>
==============================================================================================
Index.jsp
-----------
<html>
<head>
<%@include file="./base.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
</head>
<body>
<div class="container mt-3">
<div class="row">
<div class="col-md-12">
<h1 class="text-center mb-3">Welcome to Product App</h1>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">S.No</th>
<th scope="col">Product Name</th>
<th scope="col">Desc</th>
<th scope="col">Price</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${products }" var="a">
<tr>
<th scope="row">${a.id }</th>
<td>${a.name }</td>
<td>${a.description }</td>
<td>${a.price }</td>
<td><a href="delete/${a.id }"><i class="fa-solid fa-delete-left"></i></a></td>
<td><a href="update/${a.id }"><i class="fa-solid fa-pen-clip"></i></a></td>
</tr>
</c:forEach>
</tbody>
</table>

<div class="container text-center">
<a href="add-product" class="btn btn-outline-success">Add
Product</a>
</div>

</div>
</div>
</div>
</body>
</html>
===============================================================================================
add_product_form.jsp
---------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@include file="./base.jsp"%>
</head>
<body>

<h1>This is ad product form</h1>

<div class="container mt-3">
<div class="row">
<div class="col -md-6 offset-md-3">
<h1 class="text-center mb-3">Fill the product detail</h1>
<form action="handle-product" method="post">
<div class="form-group">
<label for="name ">Product Name</label> <input type="text"
class="form-control" id="name" aria-describedby="emailHelp"
name="name" placeholder="Enter the product name here">
</div>
<div class="form-group">
<label for="description">Product Description</label>
<textarea class="form-control" name="description" id="description"
rows="5" placeholder="Enter the product description"></textarea>
</div>

<div class="form-group">
<label for="price">Product Price</label> <input type="text"
placeholder="Enter Product Price" name="price"
class="form-control" id="price">
</div>

<div class="container text-center ">
<a href="${pageContext.request.contextPath }/"
class="btn btn-outline-danger">Back</a>
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
==============================================================================================
update_form.jsp
-----------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@include file="./base.jsp"%>
</head>
<body>

<div class="container mt-3">
<div class="row">
<div class="col -md-6 offset-md-3">
<h1 class="text-center mb-3">Change Product Details</h1>
<input type="text" value="${product.id }" name="id" />
<form action="${pageContext.request.contextPath }/handle-product" method="post">
<div class="form-group">
<label for="name ">Product Name</label> <input type="text"
class="form-control" id="name" aria-describedby="emailHelp"
name="name" placeholder="Enter the product name here"
value="${product.name }"
>
</div>
<div class="form-group">
<label for="description">Product Description</label>
<textarea class="form-control" name="description" id="description"
rows="5" placeholder="Enter the product description">${product.description }</textarea>
</div>

<div class="form-group">
<label for="price">Product Price</label> <input type="text"
placeholder="Enter Product Price" name="price"
class="form-control" id="price"
value="${product.price }"
>
</div>

<div class="container text-center ">
<a href="${pageContext.request.contextPath }/"
class="btn btn-outline-danger">Back</a>
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
===============================================================================================
maincontroller.java
-------------------------
package prodictcrudapp.Controller;

import java.util.List;

import javax.print.DocFlavor.STRING;
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 prodictcrudapp.Dao.ProductDao;
import prodictcrudapp.model.Product;

@Controller
public class MainController {

@Autowired
private ProductDao productDao;

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

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

//Handle Add product form
@RequestMapping( value = "/handle-product",method = RequestMethod.POST)
public String handleProduct(@ModelAttribute Product product,HttpServletRequest req,Model m)
{
System.out.println(product);
productDao.createProduct(product);
List<Product> products=productDao.getProducts();
m.addAttribute("products", products);
return "index";
}


//delete handler
@RequestMapping("/delete/{productId}")
public RedirectView deleteProduct(@PathVariable("productId") int productId,HttpServletRequest req)
{
this.productDao.deleteProduct(productId);
RedirectView redirectView = new RedirectView();
redirectView.setUrl(req.getContextPath() + "/");
return redirectView;
}

//update handler
@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";
}


}
========================================================================================
     
 
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.