diff --git a/java/spring/README.md b/java/spring/README.md
index 91d2feb..8886b2c 100644
--- a/java/spring/README.md
+++ b/java/spring/README.md
@@ -39,7 +39,7 @@ Before running this sample make sure that:
- ``` java -jar ncache-spring-0.0.1-SNAPSHOT.war ```
- After running this sample application for the first time a table with a name book will be created in a
- database. Initially, the table will empty there won't be any data, so there are two ways to create a new book:
+ database. Using the web browser, visit http://localhost:8080/bookstore/. Initially, the table will empty there won't be any data, so there are two ways to create a new book:
- either by creating a book one by one from the sample application by clicking on a Create new book button,
from this you can also check each book you have created is also cached in NCache by checking the cache count.
- A quick way to add books to the table is by running an SQL script(sqlDataScript.sql) shipped with the sample,
diff --git a/java/spring/pom.xml b/java/spring/pom.xml
index 90c191a..0e25a24 100644
--- a/java/spring/pom.xml
+++ b/java/spring/pom.xml
@@ -51,7 +51,7 @@
com.alachisoft.ncache
ncache-spring
- 5.2.0
+ 5.2.1
@@ -90,10 +90,20 @@
org.springframework.boot
spring-boot-maven-plugin
+
+
+ org.apache.maven.plugins
+ maven-dependency-plugin
+ 3.1.2
+ copy-dependencies
+ prepare-package
+
+ copy-dependencies
+
- com.alachisoft.ncache.springbootsample.SpringBootSampleApplication
+ ${project.build.directory}
diff --git a/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/CachingConfiguration.java b/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/CachingConfiguration.java
index 2084aa4..1de26ed 100644
--- a/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/CachingConfiguration.java
+++ b/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/CachingConfiguration.java
@@ -15,8 +15,6 @@ public class CachingConfiguration
public CacheManager cacheManager()
{
SpringConfigurationManager springConfigurationManager = new SpringConfigurationManager();
- URL resource = getClass().getClassLoader().getResource("ncache-spring.xml");
- springConfigurationManager.setConfigFile(resource.getPath());
NCacheCacheManager cacheManager = new NCacheCacheManager();
cacheManager.setSpringConfigurationManager(springConfigurationManager);
return cacheManager;
diff --git a/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/Book.java b/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/Book.java
index 7636c25..f23860c 100644
--- a/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/Book.java
+++ b/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/Book.java
@@ -5,14 +5,10 @@
import java.util.Date;
@Entity
-@org.hibernate.annotations.NamedQuery(name = "Book.findBookByIsbn",
-query = "select b from Book b where b.isbn = ?1")
public class Book implements Serializable {
+ // @GeneratedValue(strategy = GenerationType.AUTO)
@Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Integer id;
-
private long isbn;
private String title;
@@ -23,18 +19,10 @@ public long getIsbn() {
return isbn;
}
- public void setId(Integer id) {
- this.id = id;
- }
-
public void setIsbn(long isbn) {
this.isbn = isbn;
}
- public Integer getId() {
- return id;
- }
-
public Book(long isbn, String title, String subTitle,
String author, Date publishedDate, String publisher,
long pages, String description, String webURL) {
@@ -64,14 +52,6 @@ public Book(long isbn, String title, String subTitle,
private String webURL;
- public long getISBN() {
- return isbn;
- }
-
- public void setISBN(long isbn) {
- this.isbn = isbn;
- }
-
public String getTitle() {
return title;
}
diff --git a/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/controller/BookController.java b/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/controller/BookController.java
index 77fae14..a841d44 100644
--- a/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/controller/BookController.java
+++ b/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/controller/BookController.java
@@ -13,6 +13,7 @@
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
+import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -51,9 +52,9 @@ public String saveBook(@ModelAttribute("book") Book book, BindingResult result)
}
@RequestMapping(value = "/bookstore/edit", method = RequestMethod.GET)
- public String showEditBookPage(@RequestParam int id, ModelMap model) {
+ public String showEditBookPage(@RequestParam long id, ModelMap model) {
Book book = booksService.get(id);
- model.put("book", book);
+ model.addAttribute("book", book == null ? new Book() : book);
return "newBook";
}
@@ -66,8 +67,9 @@ public String updateBook(@ModelAttribute("book") Book book, BindingResult result
return "redirect:/bookstore";
}
+ @Transactional
@RequestMapping(value = "/bookstore/delete")
- public String deleteBook(@RequestParam int id) {
+ public String deleteBook(@RequestParam long id) {
booksService.delete(id);
return "redirect:/bookstore";
}
@@ -75,7 +77,7 @@ public String deleteBook(@RequestParam int id) {
@RequestMapping(value="/bookstore", method = RequestMethod.POST)
public ModelAndView findBook(ModelMap model, @RequestParam long isbn){
- Book book = booksService.findBookByIsbn(isbn);
+ Book book = booksService.get(isbn);
if (book == null) {
return returnError(model, isbn);
@@ -87,6 +89,7 @@ public ModelAndView findBook(ModelMap model, @RequestParam long isbn){
private ModelAndView returnError(ModelMap model, long isbn) {
String errorMessage = "The book with ISBN: " + isbn + " is not available.";
model.put("errorMessage", errorMessage);
- return new ModelAndView(new RedirectView("bookstore"));
+ //return new ModelAndView(new RedirectView("bookstore", true));
+ return homePage((Model) model);
}
}
\ No newline at end of file
diff --git a/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/repository/BookRepository.java b/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/repository/BookRepository.java
index 3726cdb..a700fd4 100644
--- a/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/repository/BookRepository.java
+++ b/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/repository/BookRepository.java
@@ -5,13 +5,13 @@
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
+import javax.transaction.Transactional;
import java.util.List;
@Repository
-public interface BookRepository extends CrudRepository {
- Book findById(int id);
+public interface BookRepository extends CrudRepository {
+ Book findByIsbn(long isbn);
@NonNull
List findAll();
- void deleteById(int id);
- Book findBookByIsbn(long isbn);
+ void deleteByIsbn(long isbn);
}
\ No newline at end of file
diff --git a/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/service/BookService.java b/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/service/BookService.java
index c520927..98ea24b 100644
--- a/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/service/BookService.java
+++ b/java/spring/src/main/java/com/alachisoft/ncache/springbootsample/bookstore/service/BookService.java
@@ -20,26 +20,21 @@ public List listAll() {
return repo.findAll();
}
- @CachePut(value = "books", key = "#book.getISBN()")
- public void save(Book book) {
- repo.save(book);
+ @CachePut(value = "books", key = "#book.isbn")
+ public Book save(Book book) {
+ return repo.save(book);
}
- @CachePut(value = "books", key = "#book.getISBN()")
- public void update(Book book) { repo.save(book); }
-
- @Cacheable(value = "books", key = "#id")
- public Book get(int id) {
- return repo.findById(id);
- }
+ @CachePut(value = "books", key = "#book.isbn")
+ public Book update(Book book) { return repo.save(book); }
@Cacheable(value = "books", key = "#isbn")
- public Book findBookByIsbn(long isbn) {
- return repo.findBookByIsbn(isbn);
+ public Book get(long isbn) {
+ return repo.findByIsbn(isbn);
}
@CacheEvict(value = "books", allEntries = true)
- public void delete(int id) {
- repo.deleteById(id);
+ public void delete(long isbn) {
+ repo.deleteByIsbn(isbn);
}
}
diff --git a/java/spring/src/main/resources/ncache-spring.xml b/java/spring/src/main/resources/ncache-spring.xml
index d059f02..82fa7f9 100644
--- a/java/spring/src/main/resources/ncache-spring.xml
+++ b/java/spring/src/main/resources/ncache-spring.xml
@@ -1,5 +1,5 @@
-
+
diff --git a/java/spring/src/main/resources/sqlDataScript.sql b/java/spring/src/main/resources/sqlDataScript.sql
index c422656..24b1f7a 100644
--- a/java/spring/src/main/resources/sqlDataScript.sql
+++ b/java/spring/src/main/resources/sqlDataScript.sql
@@ -1,22 +1,43 @@
-INSERT INTO sakila.book (id, author, description, isbn, pages, published_date, publisher, sub_title, title, weburl)
-VALUES (1, 'Marijn Haverbeke', 'JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.',
- 9781593275846, 472, '2014-12-14', 'No Starch Press', 'A Modern Introduction to Programming', 'Eloquent JavaScript, Second Edition', 'http://eloquentjavascript.net/');
-
-INSERT INTO sakila.book (id, author, description, isbn, pages, published_date, publisher, sub_title, title, weburl)
-VALUES (2, 'Addy Osmani', 'With Learning JavaScript Design Patterns, you\'ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.',
- 9781449331818, 254, '2012-07-01', 'O\'Reilly Media', 'A JavaScript and jQuery Developer\'s Guide', 'Learning JavaScript Design Patterns', 'http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/');
-
-INSERT INTO sakila.book (id, author, description, isbn, pages, published_date, publisher, sub_title, title, weburl)
-VALUES (3, 'Axel Rauschmayer', 'Like it or not, JavaScript is everywhere these days-from browser to server to mobile-and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.',
- 9781449365035, 460, '2014-02-01', 'O\'Reilly Media', 'An In-Depth Guide for Programmers', 'Speaking JavaScript', 'http://speakingjs.com/');
-
-INSERT INTO sakila.book (id, author, description, isbn, pages, published_date, publisher, sub_title, title, weburl)
-VALUES (4, 'Eric Elliott', 'Take advantage of JavaScript\'s power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that\'s easier-yes, easier-to work with as your code base grows.',
- 9781491950296, 254, '2014-07-01', 'O\'Reilly Media', 'Robust Web Architecture with Node, HTML5, and Modern JS Libraries', 'Programming JavaScript Applications', 'http://chimera.labs.oreilly.com/books/1234000000262/index.html');
-
-INSERT INTO sakila.book (id, author, description, isbn, pages, published_date, publisher, sub_title, title, weburl)
-VALUES (5, 'Nicholas C. Zakas', 'ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript.',
- 9781593277574, 352, '2016-09-03', 'No Starch Press', 'The Definitive Guide for JavaScript Developers', 'Understanding ECMAScript 6', 'https://leanpub.com/understandinges6/read');
+INSERT INTO sakila.book (author, description, id, pages, published_date, publisher, sub_title, title, weburl)
+VALUES (''Marijn Haverbeke'', ''JavaScript lies at the heart of almost every modern web application, from social apps to
+ the newest browser - based games.Though simple for beginners to pick up and play with, JavaScript is a flexible,
+ complex language that you can use to build full-scale applications.'',
+ 9781593275846, 472, ''2014-12-14'', ''No Starch Press'', ''A Modern Introduction to Programming'',
+ ''Eloquent JavaScript, Second Edition '', ''http://eloquentjavascript.net/'');
+
+INSERT INTO sakila.book (author, description, id, pages, published_date, publisher, sub_title, title, weburl)
+VALUES (''Addy Osmani'', ''With Learning JavaScript Design Patterns, you\''ll learn how to write beautiful,
+ structured, and maintainable JavaScript by applying classical and modern design patterns to the language.If you
+ want to keep your code efficient, more manageable, and up - to - date with the latest best practices,
+ this book is for you.'',
+ 9781449331818, 254, ''2012-07-01'', ''O\''Reilly Media'', ''A JavaScript and jQuery Developer\''s Guide'',
+ ''Learning JavaScript Design Patterns'',
+ ''http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/'');
+
+INSERT INTO sakila.book (author, description, id, pages, published_date, publisher, sub_title, title, weburl)
+VALUES (''Axel Rauschmayer'', '' Like it or not,
+ JavaScript is everywhere these days-from browser to server to mobile-and now you, too,
+ need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript,
+ written by a veteran programmer who once found himself in the same position.'',
+ 9781449365035, 460, ''2014-02-01'', ''O\''Reilly Media'', ''An In-Depth Guide for Programmers'',
+ ''Speaking JavaScript'', ''http://speakingjs.com/'');
+
+INSERT INTO sakila.book (author, description, id, pages, published_date, publisher, sub_title, title, weburl)
+VALUES (''Eric Elliott'',
+ ''Take advantage of JavaScript\''s power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book,
+ experienced JavaScript developers will learn how to write flexible and resilient code that\''s easier-yes,
+ easier - to work with as your code base grows.'',
+ 9781491950296, 254, ''2014-07-01'', ''O\''Reilly Media'', ''Robust Web Architecture with Node, HTML5, and Modern
+ JS Libraries '', ''Programming JavaScript Applications'',
+ ''http://chimera.labs.oreilly.com/books/1234000000262/index.html'');
+
+INSERT INTO sakila.book (author, description, id, pages, published_date, publisher, sub_title, title, weburl)
+VALUES (''Nicholas C. Zakas'',
+ ''ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6,
+ expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting
+ changes that ECMAScript 6 brings to JavaScript.'',
+ 9781593277574, 352, ''2016-09-03'', ''No Starch Press'', ''The Definitive Guide for JavaScript Developers'',
+ ''Understanding ECMAScript 6'', ''https://leanpub.com/understandinges6/read'');
diff --git a/java/spring/src/main/webapp/WEB-INF/jsp/bookdetails.jsp b/java/spring/src/main/webapp/WEB-INF/jsp/bookdetails.jsp
index 10bfb7e..3baf806 100644
--- a/java/spring/src/main/webapp/WEB-INF/jsp/bookdetails.jsp
+++ b/java/spring/src/main/webapp/WEB-INF/jsp/bookdetails.jsp
@@ -1,4 +1,5 @@
-<%@ page import="java.util.Map" %>
+<%@ page import="com.alachisoft.ncache.springbootsample.bookstore.Book" %>
+<%@ page import="com.alachisoft.ncache.springbootsample.bookstore.Book" %>
<%@ page import="com.alachisoft.ncache.springbootsample.bookstore.Book" %>
<%@page contentType="text/html" pageEncoding="UTF-8" %>
@@ -28,7 +29,7 @@
Book book = (Book) result;
%>
- | <% out.println(book.getISBN()); %> |
+ <% out.println(book.getIsbn()); %> |
<% out.println(book.getTitle()); %> |
<% out.println(book.getSubTitle()); %> |
<% out.println(book.getPages()); %> |
diff --git a/java/spring/src/main/webapp/WEB-INF/jsp/bookstore.jsp b/java/spring/src/main/webapp/WEB-INF/jsp/bookstore.jsp
index 97cff91..f27c1e8 100644
--- a/java/spring/src/main/webapp/WEB-INF/jsp/bookstore.jsp
+++ b/java/spring/src/main/webapp/WEB-INF/jsp/bookstore.jsp
@@ -38,21 +38,23 @@
${book.author} |
${book.publisher} |
Update
+ href="${pageContext.request.contextPath}/bookstore/edit?id=${book.isbn}">Update
Delete |
+ href="${pageContext.request.contextPath}/bookstore/delete/?id=${book.isbn}">Delete
- <%
- Object error = request.getAttribute("errorMessage");
- if(error != null){
- out.println(error);
- }
- %>
+
+ <%
+ Object error = request.getAttribute("errorMessage");
+ if(error != null){
+ out.println((String) error);
+ }
+ %>
+
diff --git a/java/spring/src/main/webapp/WEB-INF/jsp/error.jsp b/java/spring/src/main/webapp/WEB-INF/jsp/error.jsp
new file mode 100644
index 0000000..4750d5f
--- /dev/null
+++ b/java/spring/src/main/webapp/WEB-INF/jsp/error.jsp
@@ -0,0 +1,43 @@
+<%--
+ Created by IntelliJ IDEA.
+ User: saad_farooq
+ Date: 14/09/2021
+ Time: 6:06 PM
+ To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+
+
+ Error
+
+
+
+
+
+
+
diff --git a/java/spring/src/main/webapp/WEB-INF/jsp/newBook.jsp b/java/spring/src/main/webapp/WEB-INF/jsp/newBook.jsp
index 5b871a7..425d7e3 100644
--- a/java/spring/src/main/webapp/WEB-INF/jsp/newBook.jsp
+++ b/java/spring/src/main/webapp/WEB-INF/jsp/newBook.jsp
@@ -22,12 +22,11 @@
Add Book
-
|