📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Before you get started, check out Spring Data JPA Tutorial and Spring MVC Tutorial
Spring MVC Tutorials and Articles
- Spring MVC 5 - Hello World Example
- Spring MVC 5 - Sign Up Form Handling Example
- Spring MVC JSP Form Tags Tutorial
- Spring MVC 5 Form Validation with Annotations Tutorial
- Spring MVC 5 + Hibernate 5 + JSP + MySQL CRUD Tutorial
- Spring MVC 5 + Spring Data JPA + Hibernate 5 + JSP + MySQL Tutorial
- Spring MVC + Spring Boot2 + JSP + JPA + Hibernate 5 + MySQL Example
- Spring Boot 2 MVC Web Application Thymeleaf JPA MySQL Example
- Spring MVC + Spring Boot2 + JSP + JPA + Hibernate 5 + MySQL Example
- Spring Boot 2 MVC Web Application Thymeleaf JPA MySQL Example
- Spring Boot 2 - Spring MVC + Thymeleaf Input Form Validation
- Spring Boot 2 + Spring MVC + Spring Security + JPA + Thymeleaf + MySQL Tutorial
- Authenticating a User with LDAP using Spring Boot and Spring Security
- The Spring @Controller and @RestController Annotations with Examples
- Spring @RequestBody and @ResponseBody Annotations
- @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping
- Spring Web MVC Annotations
- Mini Todo Management Project using Spring Boot + Spring MVC + Spring Security
- User Registration Module using Spring Boot + Spring MVC + Spring Security + Hibernate 5
Tools and technologies used
- Spring MVC - 5.1.0 RELEASE
- Spring Data JPA - 2.10 RELEASE
- Hibernate - 5.2.17.Final
- JDK - 1.8 or later
- Maven - 3.5.1
- Apache Tomcat - 8.5
- IDE - STS/Eclipse Neon.3
- JSTL - 1.2.1
- JSP - 2.3.1
Development Steps
- Create a Maven Web Application
- Add Dependencies - pom.xml File
- Project Structure
- AppInitializer - Register a DispatcherServlet using Java-based Spring configuration
- PersistenceJPAConfig - Spring Data JPA and Hibernate Configuration using Java-based Spring configuration
- WebMvcConfig - Spring MVC Bean Configuration using Java-based Spring configuration
- JPA Entity - Customer.java
- Spring MVC Controller Class - CustomerController.java
- Service Layer - CustomerService.java and CustomerServiceImpl.java
- Spring Data JPA Repository - CustomerRepository.java
- Custom Exception - ResourceNotFoundException.java
- JSP Views - customer-form.jsp and list-customers.jsp
- Serve Static Resources - CSS and JS
- Build and Run an application
- Demo
1. Create Maven Web Application
If you are new to maven then learn maven on Apache Maven Tutorial
2. Add Dependencies - pom.xml File
<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>net.javaguides.springmvc</groupId>
<artifactId>springmvc5-springdatajpa2-jsp-mysql-example</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc5-springdatajpa2-jsp-mysql-example Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
<spring.version>5.1.0.RELEASE</spring.version>
<hibernate.version>5.2.17.Final</hibernate.version>
<hibernate.validator>5.4.1.Final</hibernate.validator>
<c3p0.version>0.9.5.2</c3p0.version>
<jstl.version>1.2.1</jstl.version>
<tld.version>1.1.2</tld.version>
<servlets.version>3.1.0</servlets.version>
<jsp.version>2.3.1</jsp.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<!-- Spring MVC Dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Hibernate Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!-- JSTL Dependency -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${tld.version}</version>
</dependency>
<!-- Servlet Dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlets.version}</version>
<scope>provided</scope>
</dependency>
<!-- JSP Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.20</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. Project Structure
Class Diagram
4. AppInitializer - Register a DispatcherServlet using Java-based Spring configuration
package net.javaguides.springmvc.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* @author Ramesh Fadatare
*/
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class << ? > [] getRootConfigClasses() {
return new Class[] {
PersistenceJPAConfig.class
};
//return null;
}
@Override
protected Class << ? > [] getServletConfigClasses() {
return new Class[] {
WebMvcConfig.class
};
}
@Override
protected String[] getServletMappings() {
return new String[] {
"/"
};
}
}
5. PersistenceJPAConfig - Spring Data JPA and Hibernate Configuration using Java-based Spring configuration
Database Configuration - database.properties
# jdbc.X
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo?useSSL=false
jdbc.user=root
jdbc.pass=root
# hibernate.X
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false
PersistenceJPAConfig.java
package net.javaguides.springmvc.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@PropertySource({
"classpath:database.properties"
})
@ComponentScan({
"net.javaguides.springmvc"
})
@EnableJpaRepositories(basePackages = "net.javaguides.springmvc.repository")
public class PersistenceJPAConfig {
@Autowired
private Environment env;
public PersistenceJPAConfig() {
super();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(new String[] {
"net.javaguides.springmvc.entity"
});
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
entityManagerFactoryBean.setJpaProperties(additionalProperties());
return entityManagerFactoryBean;
}
final Properties additionalProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
return hibernateProperties;
}
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
6. WebMvcConfig - Spring MVC Bean Configuration using Java-based Spring configuration
package net.javaguides.springmvc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
/**
* @author Ramesh Fadatare
*/
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
"net.javaguides.springmvc.controller"
})
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
7. JPA Entity - Customer.java
package net.javaguides.springmvc.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
public Customer() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Customer [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}
}
8. Spring MVC Controller Class - CustomerController.java
package net.javaguides.springmvc.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import net.javaguides.springmvc.entity.Customer;
import net.javaguides.springmvc.exception.ResourceNotFoundException;
import net.javaguides.springmvc.service.CustomerService;
@Controller
@RequestMapping("/customer")
public class CustomerController {
private static final Logger LOG = LoggerFactory.getLogger(CustomerController.class);
@Autowired
private CustomerService customerService;
@GetMapping("/list")
public String listCustomers(Model theModel) {
List < Customer > theCustomers = customerService.getCustomers();
theModel.addAttribute("customers", theCustomers);
return "list-customers";
}
@GetMapping("/showForm")
public String showFormForAdd(Model theModel) {
LOG.debug("inside show customer-form handler method");
Customer theCustomer = new Customer();
theModel.addAttribute("customer", theCustomer);
return "customer-form";
}
@PostMapping("/saveCustomer")
public String saveCustomer(@ModelAttribute("customer") Customer theCustomer) {
customerService.saveCustomer(theCustomer);
return "redirect:/customer/list";
}
@GetMapping("/updateForm")
public String showFormForUpdate(@RequestParam("customerId") int theId,
Model theModel) throws ResourceNotFoundException {
Customer theCustomer = customerService.getCustomer(theId);
theModel.addAttribute("customer", theCustomer);
return "customer-form";
}
@GetMapping("/delete")
public String deleteCustomer(@RequestParam("customerId") int theId) throws ResourceNotFoundException {
customerService.deleteCustomer(theId);
return "redirect:/customer/list";
}
}
9. Service Layer - CustomerService.java and CustomerServiceImpl.java
CustomerService.java
package net.javaguides.springmvc.service;
import java.util.List;
import net.javaguides.springmvc.entity.Customer;
import net.javaguides.springmvc.exception.ResourceNotFoundException;
public interface CustomerService {
public List < Customer > getCustomers();
public void saveCustomer(Customer theCustomer);
public Customer getCustomer(int theId) throws ResourceNotFoundException;
public void deleteCustomer(int theId) throws ResourceNotFoundException;
}
CustomerServiceImpl.java
package net.javaguides.springmvc.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import net.javaguides.springmvc.entity.Customer;
import net.javaguides.springmvc.exception.ResourceNotFoundException;
import net.javaguides.springmvc.repository.CustomerRepository;
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerRepository customerRepository;
@Override
@Transactional
public List < Customer > getCustomers() {
return customerRepository.findAll();
}
@Override
@Transactional
public void saveCustomer(Customer theCustomer) {
customerRepository.save(theCustomer);
}
@Override
@Transactional
public Customer getCustomer(int id) throws ResourceNotFoundException {
return customerRepository.findById(id).orElseThrow(
() - > new ResourceNotFoundException(id));
}
@Override
@Transactional
public void deleteCustomer(int theId) {
customerRepository.deleteById(theId);
}
}
10. Spring Data JPA Repository - CustomerRepository.java
package net.javaguides.springmvc.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import net.javaguides.springmvc.entity.Customer;
@Repository("customerRepository")
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
}
11. Custom Exception - ResourceNotFoundException.java
package net.javaguides.springmvc.exception;
public class ResourceNotFoundException extends Exception {
private static final long serialVersionUID = 1 L;
public ResourceNotFoundException(Object resourId) {
super(resourId != null ? resourId.toString() : null);
}
}
12. JSP Views - customer-form.jsp and list-customers.jsp
customer-form.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC 5 - form handling | Java Guides</title>
<link href="<c:url value="/resources/css/bootstrap.min.css" />"
rel="stylesheet">
<script src="<c:url value="/resources/js/jquery-1.11.1.min.js" />"></script>
<script src="<c:url value="/resources/js/bootstrap.min.js" />"></script>
</head>
<body>
<div class="container">
<div class="col-md-offset-2 col-md-7">
<h3 class="text-center">Spring MVC 5 + Spring Data JPA 2 + JSP + MySQL
Example - Customer Management</h3>
<div class="panel panel-info">
<div class="panel-heading">
<div class="panel-title">Add Customer</div>
</div>
<div class="panel-body">
<form:form action="saveCustomer" cssClass="form-horizontal"
method="post" modelAttribute="customer">
<!-- need to associate this data with customer id -->
<form:hidden path="id" />
<div class="form-group">
<label for="firstname" class="col-md-3 control-label">First
Name</label>
<div class="col-md-9">
<form:input path="firstName" cssClass="form-control" />
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-md-3 control-label">Last
Name</label>
<div class="col-md-9">
<form:input path="lastName" cssClass="form-control" />
</div>
</div>
<div class="form-group">
<label for="email" class="col-md-3 control-label">Email</label>
<div class="col-md-9">
<form:input path="email" cssClass="form-control" />
</div>
</div>
<div class="form-group">
<!-- Button -->
<div class="col-md-offset-3 col-md-9">
<form:button cssClass="btn btn-primary">Submit</form:button>
</div>
</div>
</form:form>
</div>
</div>
</div>
</div>
</body>
</html>
list-customers.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>javaguides.net</title>
<link href="<c:url value="/resources/css/bootstrap.min.css" />"
rel="stylesheet">
<%@ page isELIgnored="false"%>
<script src="<c:url value="/resources/js/jquery-1.11.1.min.js" />"></script>
<script src="<c:url value="/resources/js/bootstrap.min.js" />"></script>
</head>
<body>
<div class="container">
<div class="col-md-offset-1 col-md-10">
<h3 class="text-center">Spring MVC 5 + Spring Data JPA 2 + JSP +
MySQL Example - Customer Management</h3>
<hr />
<input type="button" value="Add Customer"
onclick="window.location.href='showForm'; return false;"
class="btn btn-primary" /> <br />
<br />
<div class="panel panel-info">
<div class="panel-heading">
<div class="panel-title">Customer List</div>
</div>
<div class="panel-body">
<table class="table table-striped table-bordered">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Action</th>
</tr>
<!-- loop over and print our customers -->
<c:forEach var="tempCustomer" items="${customers}">
<!-- construct an "update" link with customer id -->
<c:url var="updateLink" value="/customer/updateForm">
<c:param name="customerId" value="${tempCustomer.id}" />
</c:url>
<!-- construct an "delete" link with customer id -->
<c:url var="deleteLink" value="/customer/delete">
<c:param name="customerId" value="${tempCustomer.id}" />
</c:url>
<tr>
<td>${tempCustomer.firstName}</td>
<td>${tempCustomer.lastName}</td>
<td>${tempCustomer.email}</td>
<td>
<!-- display the update link --> <a href="${updateLink}">Update</a>
| <a href="${deleteLink}"
onclick="if (!(confirm('Are you sure you want to delete this customer?'))) return false">Delete</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</div>
</div>
<div class="footer">
<p>Footer</p>
</div>
</body>
</html>
13. Serve Static Resources - CSS and JS
- Create a resource folder under webapp directory.
- Create css and js folders under the resource directory.
- Download and keep bootstrap.min.css file under css folder
- download and keep bootstrap.min.js and jquery-1.11.1.min.js files under the resource directory. Note that bootstrap js is depends on jquery js.
14. Build and Run an application
clean install
Comments
Post a Comment
Leave Comment