File tree Expand file tree Collapse file tree 8 files changed +129
-9
lines changed
Expand file tree Collapse file tree 8 files changed +129
-9
lines changed Original file line number Diff line number Diff line change 4949 <artifactId >spring-boot-starter-test</artifactId >
5050 <scope >test</scope >
5151 </dependency >
52+ <dependency >
53+ <groupId >org.springframework.boot</groupId >
54+ <artifactId >spring-boot-starter-web</artifactId >
55+ <scope >runtime</scope >
56+ </dependency >
57+ <dependency >
58+ <groupId >org.springframework</groupId >
59+ <artifactId >spring-web</artifactId >
60+ <version >5.0.5.RELEASE</version >
61+ </dependency >
5262 </dependencies >
5363
5464 <build >
Original file line number Diff line number Diff line change 1- package com .sscodes ;
1+ package com .sscodes .controller ;
2+
3+ import com .sscodes .model .Book ;
4+ import com .sscodes .services .BookService ;
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .stereotype .Controller ;
7+ import org .springframework .ui .Model ;
8+ import org .springframework .web .bind .annotation .*;
29
310/**
11+ * Controller for Book
12+ * This will be responsible for handling incoming traffic for books
13+ *
414 * Created by sam on 8/6/18.
515 */
16+ @ Controller
617public class BookController {
18+
19+ @ Autowired
20+ private BookService bookService ;
21+
22+ @ RequestMapping (value = "/books" , method = RequestMethod .GET )
23+ public String getAllBooks (Model model ){
24+ model .addAttribute ("books" , bookService .getAllBooks ());
25+ return "books" ;
26+ }
27+
28+ @ RequestMapping (value = "/book" , method = RequestMethod .GET )
29+ public String addBook (Book book ){
30+ return "book" ;
31+ }
32+
33+ @ RequestMapping (value = "/books" , method = RequestMethod .POST )
34+ public String addNewBook (@ ModelAttribute Book book , Model model ){
35+ bookService .addNewBook (book );
36+ model .addAttribute ("books" , bookService .getAllBooks ());
37+ return "books" ;
38+ }
739}
Original file line number Diff line number Diff line change 11package com .sscodes .model ;
22
3+ import javax .persistence .*;
4+
35/**
6+ * Pojo class for book
7+ *
48 * Created by sam on 8/6/18.
59 */
10+ @ Entity
611public class Book {
12+
13+ @ Id
14+ @ GeneratedValue (strategy = GenerationType .SEQUENCE , generator = "book_seq" )
15+ @ SequenceGenerator (name = "book_seq" , sequenceName = "book_seq" )
16+ private long id ;
17+ private String bookName ;
18+ private String isbn ;
19+
20+ public Book () {
21+ }
22+
23+ public Book (String bookName , String isbn ) {
24+ this .bookName = bookName ;
25+ this .isbn = isbn ;
26+ }
27+
28+ public String getBookName () {
29+ return bookName ;
30+ }
31+
32+ public void setBookName (String bookName ) {
33+ this .bookName = bookName ;
34+ }
35+
36+ public String getIsbn () {
37+ return isbn ;
38+ }
39+
40+ public void setIsbn (String isbn ) {
41+ this .isbn = isbn ;
42+ }
43+
44+ public long getId () {
45+ return id ;
46+ }
747}
Original file line number Diff line number Diff line change 99 * Created by sam on 8/6/18.
1010 */
1111
12- public interface bookRepository extends CrudRepository <Book , Long >{
12+ public interface BookRepository extends CrudRepository <Book , Long >{
1313}
Original file line number Diff line number Diff line change 11package com .sscodes .services ;
22
3+ import com .sscodes .model .Book ;
4+ import com .sscodes .repositories .BookRepository ;
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .stereotype .Service ;
7+
8+ import java .util .List ;
9+
310/**
11+ * Service class will be responsible for handling book service
12+ *
413 * Created by sam on 8/6/18.
514 */
15+ @ Service
616public class BookService {
17+
18+ @ Autowired
19+ private BookRepository bookRepository ;
20+
21+ public Iterable <Book > getAllBooks (){
22+ return bookRepository .findAll ();
23+ }
24+
25+ public void addNewBook (Book book ){
26+ bookRepository .save (book );
27+ }
728}
Original file line number Diff line number Diff line change 1+ spring.h2.console.enabled =true
Original file line number Diff line number Diff line change 11<!DOCTYPE html>
2- < html lang ="en ">
2+ < html lang ="en " xmlns:th =" http://www.thymeleaf.org " >
33< head >
44 < meta charset ="UTF-8 ">
5- < title > $ Title$ </ title >
5+ < title > Title</ title >
66</ head >
77< body >
8- $END$
8+ < form action ="# " th:action ="@{/books} " th:object ="${book} " method ="post ">
9+ < p > Name: < input type ="text " th:field ="*{bookName} " /> </ p >
10+ < p > ISBN: < input type ="text " th:field ="*{isbn} " /> </ p >
11+ < p > < input type ="submit " value ="Submit " /> < input type ="reset " value ="Reset " /> </ p >
12+ </ form >
913</ body >
1014</ html >
Original file line number Diff line number Diff line change 11<!DOCTYPE html>
2- < html lang ="en ">
2+ < html lang ="en " xmlns:th =" http://www.thymeleaf.org " >
33< head >
4- < meta charset =" UTF-8 ">
5- < title > $ Title$ </ title >
4+ < meta http-equiv =" Content-Type " content =" text/html; charset= UTF-8 " / >
5+ < title > Title</ title >
66</ head >
77< body >
8- $END$
8+ < table >
9+ < tr >
10+ < th > ID</ th >
11+ < th > NAME</ th >
12+ < th > ISBN</ th >
13+ </ tr >
14+ < tr th:each ="book : ${books} ">
15+ < td th:text ="${book.id} "> id</ td >
16+ < td th:text ="${book.bookName} "> id</ td >
17+ < td th:text ="${book.isbn} "> id</ td >
18+ </ tr >
19+ </ table >
20+
921</ body >
1022</ html >
You can’t perform that action at this time.
0 commit comments