web mvc running notes
web mvc running notes
web mvc running notes
================
Spring Web MVC
================
Web app : C 2 B
Distributed App : B 2 B
=============================
Spring Web MVC Architecture
=============================
4) Model And View : model represents data & view represents UI page
-----------------------------------------------------------------------
a) web-starter
b) thymeleaf - starter
3) Create Controller method and bind it to get request using @GetMapping annotation
@Controller
@GetMapping
@RequestMapping
----------------------------------------------------------------
=====================
Today's Assignment
=====================
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
=======================
a) web-starter
b) jpa-starter
c) mysql-connector
d) thymeleaf-starter
e) devtools
-------------------------------------------------------------------
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer bookId; // book_id
private String bookName;
private Double bookPrice;
}
-----------------------------------------------------------------------
public interface BookRepository extends JpaRepository<Book, Integer>{
}
----------------------------------------------------------------------
}
------------------------------------------------------------------
@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>
</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>
-------------------------------------------------------------------------