web mvc running notes

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

================

Spring Web MVC


================
1) What is Web MVC
2) Spring MVC Architecture
3) First Web App Development
4) Embedded Servers
5) Working with Jetty server
6) Thymeleaf (UI)
7) How to send data from controller to UI
8) How to send data from UI to controller
- Query Params
- Path Params
- Request Body
- Response Body
9) How to develop Thymeleaf forms
10) Form Bindings
11) Form Validations
12) Email Sending
13) Actuators in SpringBoot

================
Spring Web MVC
================

=> It is one of the module in spring framework

=> It is used to develop both web & distributed apps

Web app : C 2 B

Distributed App : B 2 B

=> Spring Web MVC supports multiple presentation technologies

Ex : JSP and Thymleaf

=> Web MVC supports form binding

form data <--------> java obj

=============================
Spring Web MVC Architecture
=============================

1) Dispatcher Servlet : Front Controller (pre & post - processing)

2) Handler Mapper : To identify which controller should handle req

3) Controller : Responsible to handle request & provide MAV as resp

4) Model And View : model represents data & view represents UI page

5) View Resolver : Identify where view files available in project

6) View : Render (processing) model data on view page


------------------------------------------------------------------------

web-starter : web app + rest apis + spring mvc + tomcat

thymeleaf : presentation technology

-----------------------------------------------------------------------

1) Create boot app with below dependencies

a) web-starter
b) thymeleaf - starter

2) Create Spring controller using @Controller annotation

3) Create Controller method and bind it to get request using @GetMapping annotation

4) Create view page (html) under templates folder

5) Add bootstrap config to html page

@Controller
@GetMapping
@RequestMapping

----------------------------------------------------------------

=====================
Today's Assignment
=====================

1) Develop web application using springboot + thymeleaf to display welcome msg to


user in the browser.

2) Develop web application using "web mvc + data jpa" to retrieve books data from
mysql db and display books data in HTML page in table format.

1) Spring Boot
2) Spring Web MVC
3) Spring Data jpa

=======================
development procedure
=======================

1) create boot app with below dependencies

a) web-starter
b) jpa-starter
c) mysql-connector
d) thymeleaf-starter
e) devtools

2) Create Entity & repository


3) Create service interface & impl class

4) Create controller with required method

5) create view page with presentation logic

6) configure data source properties in application.propeties file

-------------------------------------------------------------------
@Entity
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer bookId; // book_id
private String bookName;
private Double bookPrice;

//setters & getters

}
-----------------------------------------------------------------------
public interface BookRepository extends JpaRepository<Book, Integer>{

}
----------------------------------------------------------------------

public interface BookService {

public List<Book> getAllBooks();

}
------------------------------------------------------------------

@Service
public class BookServiceImpl implements BookService {

@Autowired
private BookRepository bookRepo;

@Override
public List<Book> getAllBooks() {
return bookRepo.findAll();
}
}

-------------------------------------------------------------------
@Controller
public class BookController {

@Autowired
private BookService service;

@GetMapping("/books")
public ModelAndView getBooks() {
ModelAndView mav = new ModelAndView();
List<Book> allBooks = service.getAllBooks();
mav.addObject("books", allBooks);
mav.setViewName("booksView");
return mav;
}
}

---------------------------------------------------------------------
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ashok IT</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/
Dwwykc2MPK8M2HN" crossorigin="anonymous">
</head>

<body>
<div class="container">

<h1>Books Info</h1>

<a href="#" class="btn btn-primary mb-3">Add New Book</a>

<table class="table table-bordered table-striped">


<thead>
<tr>
<th>Book Id</th>
<th>Book Name</th>
<th>Book Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr th:each="b : ${books}">
<td th:text="${b.bookId}"></td>
<td th:text="${b.bookName}"></td>
<td th:text="${b.bookPrice}"></td>
<td>
<a href="#" class="btn btn-secondary">Edit</a>
<a href="#" class="btn btn-danger">Delete</a>
</td>
</tr>
<tr th:if="${books.isEmpty()}">
<td colspan="4">No Records Found</td>
</tr>
</tbody>
</table>

</div>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>
</body>

</html>
-------------------------------------------------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy