Browse Source

Use Thymeleaf message expressions in web forms

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.1-alpha
Pekka Helenius 4 years ago
parent
commit
7a22d4bb14
5 changed files with 117 additions and 60 deletions
  1. +0
    -4
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java
  2. +41
    -0
      bookstore/src/main/resources/messages.properties
  3. +32
    -22
      bookstore/src/main/resources/templates/bookadd.html
  4. +31
    -21
      bookstore/src/main/resources/templates/bookedit.html
  5. +13
    -13
      bookstore/src/main/resources/templates/booklist.html

+ 0
- 4
bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java View File

@ -35,8 +35,6 @@ public class BookController {
private static final String bookDeletePageView = "bookdelete";
private static final String bookEditPageView = "bookedit";
private static String currency_symbol = "€";
private Map<String,String> globalModelMap = new HashMap<String,String>() {
private static final long serialVersionUID = 1L;
{
@ -45,8 +43,6 @@ public class BookController {
put("addpage", bookAddPageView);
put("deletepage", bookDeletePageView);
put("editpage", bookEditPageView);
put("currency_symbol", currency_symbol);
}};
private HttpServerLogger httpServerLogger = new HttpServerLogger();


+ 41
- 0
bookstore/src/main/resources/messages.properties View File

@ -1,2 +1,43 @@
# Keep as fall back values if multi-language support is added
typeMismatch.book.year = Invalid year format
typeMismatch.book.price = Invalid price format
book.author = Author
book.title = Title
book.isbn = ISBN
book.year = Year
book.price = Price
book.category = Category
book.desc.set.author = Set book author name
book.desc.set.title = Set book primary title
book.desc.set.isbn = Set book ISBN code
book.desc.set.year = Set book publication year
book.desc.set.price = Set book price
book.desc.set.category = Select appropriate category
book.error.author = Invalid author name
book.error.title = Invalid title
book.error.isbn = Invalid ISBN code
book.error.year = Invalid year
book.error.price = Invalid price
page.title.browser.list = Bookstore page
page.title.browser.add = Add book
page.title.browser.edit = Update book
page.title.webform.list = Books
page.title.webform.add = Add new book
page.title.webform.edit = Update existing book
page.text.list.actions = Actions
page.text.list.delete = Delete
page.text.list.edit = Edit
button.book.add = Add book
button.book.edit = Update book
button.page.list.return = Return to book list page
page.symbols.currency = \u20AC

+ 32
- 22
bookstore/src/main/resources/templates/bookadd.html View File

@ -3,51 +3,61 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link type="text/css" rel="stylesheet" th:href="@{../css/bootstrap.min.css}" />
<title>Add new book</title>
<title th:text="#{page.title.browser.add}"></title>
</head>
<body>
<div class="col-md-4 mb-3">
<h1>Add new book</h1>
<h1 th:text="#{page.title.webform.add}"></h1>
<form th:object="${book}" action="#" th:action="@{__${addpage}__}" method="post">
<div class="form-group">
<label for="BookAuthor">Author</label>
<label for="BookAuthor" th:text="#{book.author}"></label>
<input class="form-control" type="text" th:field="*{author}" placeholder="Book author"/>
<small class="form-text text-muted">Set book author name</small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('author')}" th:errors="*{author}">Invalid author name</div>
<small class="form-text text-muted" th:text="#{book.desc.set.author}"></small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('author')}"
th:errors="*{author}">Invalid author name</div>
</div>
<div class="form-group">
<label for="BookTitle">Title</label>
<label for="BookTitle" th:text="#{book.title}"></label>
<input class="form-control" type="text" th:field="*{title}" placeholder="Book title"/>
<small class="form-text text-muted">Set book primary title</small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Invalid title</div>
<small class="form-text text-muted" th:text="#{book.desc.set.title}"></small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('title')}"
th:errors="*{title}">Invalid title</div>
</div>
<div class="form-group">
<label for="BookISBN">ISBN</label>
<label for="BookISBN" th:text="#{book.isbn}"></label>
<input class="form-control" type="text" th:field="*{isbn}" placeholder="Book ISBN code"/>
<small class="form-text text-muted">Set book ISBN code</small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('isbn')}" th:errors="*{isbn}">Invalid ISBN code</div>
<small class="form-text text-muted" th:text="#{book.desc.set.isbn}"></small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('isbn')}"
th:errors="*{isbn}">Invalid ISBN code</div>
</div>
<div class="form-group">
<label for="BookYear">Year</label>
<label for="BookYear" th:text="#{book.year}"></label>
<input class="form-control" type="text" th:field="*{year}" placeholder="Book publication year (YYYY)"/>
<small class="form-text text-muted">Set book publication year</small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('year')}" th:errors="*{year}">Invalid year</div>
<small class="form-text text-muted" th:text="#{book.desc.set.year}"></small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('year')}"
th:errors="*{year}">Invalid year</div>
</div>
<div class="form-group">
<label for="BookPrice">Price</label>
<label for="BookPrice" th:text="#{book.price}"></label>
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text" th:text="${currency_symbol}"></div>
<div class="input-group-text" th:text="#{page.symbols.currency}"></div>
</div>
<input class="form-control" type="text" th:field="*{price}" placeholder="Book price"/>
</div>
<small class="form-text text-muted">Set book price</small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('price')}" th:errors="*{price}">Invalid price</div>
<small class="form-text text-muted" th:text="#{book.desc.set.price}"></small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('price')}"
th:errors="*{price}">Invalid price</div>
</div>
<div class="form-group">
<label for="BookCategory">Category</label>
<label for="BookCategory" th:text="#{book.category}"></label>
<select class="form-control" th:field="*{category}">
<option
th:each="category : ${categories}"
@ -55,15 +65,15 @@
th:text="${category.getName()}"
></option>
</select>
<small class="form-text text-muted">Select appropriate category</small>
<small class="form-text text-muted" th:text="#{book.desc.set.category}"></small>
</div>
<button class="btn btn-primary" type="submit">Add book</button>
<button class="btn btn-primary" type="submit" th:text="#{button.book.add}"></button>
</form>
<br>
<form action="#" th:action="@{__${listpage}__}" method="get">
<button class="btn btn-success" type="submit">Return to book list page</button>
<button class="btn btn-success" type="submit" th:text="#{button.page.list.return}"></button>
</form>
</div>


+ 31
- 21
bookstore/src/main/resources/templates/bookedit.html View File

@ -3,48 +3,57 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link type="text/css" rel="stylesheet" th:href="@{../css/bootstrap.min.css}" />
<title>Update existing book</title>
<title th:text="#{page.title.browser.edit}"></title>
</head>
<body>
<div class="col-md-4 mb-3">
<h1>Update existing book</h1>
<h1 th:text="#{page.title.webform.edit}"></h1>
<form th:object="${book}" action="#" th:action="@{{id}(id=${book.id})}" method="post">
<div class="form-group">
<label for="BookAuthor">Author</label><br>
<label for="BookAuthor" th:text="#{book.author}"></label><br>
<input class="form-control" type="text" th:field="*{author}" placeholder="Book author"/>
<small class="form-text text-muted">Set book author name</small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('author')}" th:errors="*{author}">Invalid author name</div>
<small class="form-text text-muted" th:text="#{book.desc.set.author}"></small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('author')}"
th:errors="*{author}">Invalid author name</div>
</div>
<div class="form-group">
<label for="BookName">Title</label><br>
<label for="BookName" th:text="#{book.title}"></label><br>
<input class="form-control" type="text" th:field="*{title}" placeholder="Book title"/>
<small class="form-text text-muted">Set book primary title</small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Invalid title</div>
<small class="form-text text-muted" th:text="#{book.desc.set.title}"></small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('title')}"
th:errors="*{title}">Invalid title</div>
</div>
<div class="form-group">
<label for="BookISBN">ISBN</label><br>
<label for="BookISBN" th:text="#{book.isbn}"></label><br>
<input class="form-control" type="text" th:field="*{isbn}" placeholder="Book ISBN code"/>
<small class="form-text text-muted">Set book ISBN code</small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('isbn')}" th:errors="*{isbn}">Invalid ISBN code</div>
<small class="form-text text-muted" th:text="#{book.desc.set.isbn}"></small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('isbn')}"
th:errors="*{isbn}">Invalid ISBN code</div>
</div>
<div class="form-group">
<label for="BookYear">Year</label><br>
<label for="BookYear" th:text="#{book.year}"></label><br>
<input class="form-control" type="text" th:field="*{year}" placeholder="Book publication year (YYYY)"/>
<small class="form-text text-muted">Set book publication year</small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('year')}" th:errors="*{year}">Invalid year</div>
<small class="form-text text-muted" th:text="#{book.desc.set.year}"></small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('year')}"
th:errors="*{year}">Invalid year</div>
</div>
<div class="form-group">
<label for="fname">Price</label>
<label for="fname" th:text="#{book.price}"></label>
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text" th:text="${currency_symbol}"></div>
<div class="input-group-text" th:text="#{page.symbols.currency}"></div>
</div>
<input class="form-control" type="text" th:field="*{price}" placeholder="Book price"/>
</div>
<small class="form-text text-muted">Set book price</small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('price')}" th:errors="*{price}">Invalid price</div>
<small class="form-text text-muted" th:text="#{book.desc.set.price}"></small>
<div class="alert alert-danger mt-2" th:if="${#fields.hasErrors('price')}"
th:errors="*{price}">Invalid price</div>
</div>
<div class="form-group">
@ -56,14 +65,15 @@
th:text="${category.getName()}"
></option>
</select>
<small class="form-text text-muted">Select appropriate category</small>
<small class="form-text text-muted" th:text="#{book.desc.set.category}"></small>
</div>
<button class="btn btn-primary" type="submit">Update book</button>
<button class="btn btn-primary" type="submit" th:text="#{button.book.edit}"></button>
</form>
<br>
<form action="#" th:action="@{../__${listpage}__}" method="get">
<button class="btn btn-success" type="submit">Return to book list page</button>
<button class="btn btn-success" type="submit" th:text="#{button.page.list.return}"></button>
</form>
</div>


+ 13
- 13
bookstore/src/main/resources/templates/booklist.html View File

@ -3,21 +3,21 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link type="text/css" rel="stylesheet" th:href="@{css/bootstrap.min.css}" />
<title>Bookstore page</title>
<title th:text="#{page.title.browser.list}"></title>
</head>
<body>
<div>
<h1>Books</h1>
<h1 th:text="#{page.title.webform.list}"></h1>
<table class="table table-striped">
<tr>
<th>Author</th>
<th>Title</th>
<th>ISBN</th>
<th>Year</th>
<th>Category</th>
<th>Price</th>
<th>Actions</th>
<th th:text="#{book.author}"></th>
<th th:text="#{book.title}"></th>
<th th:text="#{book.isbn}"></th>
<th th:text="#{book.year}"></th>
<th th:text="#{book.category}"></th>
<th th:text="#{book.price}"></th>
<th th:text="#{page.text.list.actions}"></th>
<th></th>
</tr>
<tr th:each="book : ${books}">
@ -26,16 +26,16 @@
<td th:text="${book.isbn}"></td>
<td th:text="${book.year}"></td>
<td th:text="${book.category.getName()}"></td>
<td th:text="${#numbers.formatDecimal(book.price, 1, 2)}+ ' ' + ${currency_symbol}"></td>
<td th:text="${#numbers.formatDecimal(book.price, 1, 2)}+ ' ' + #{page.symbols.currency}"></td>
<td><a class="btn btn-danger"
th:attr="onclick='javascript:return confirm(\'' + 'Delete book: ' + ${book.title} + '?' + '\');'"
th:href="@{__${deletepage}__/{id}(id=${book.id})}">Delete</a></td>
th:href="@{__${deletepage}__/{id}(id=${book.id})}" th:text="#{page.text.list.delete}"></a></td>
<td><a class="btn btn-warning"
th:href="@{__${editpage}__/{id}(id=${book.id})}">Edit</a></td>
th:href="@{__${editpage}__/{id}(id=${book.id})}" th:text="#{page.text.list.edit}"></a></td>
</tr>
</table>
<div>
<a class="btn btn-success" th:href="@{__${addpage}__}">Add Book</a>
<a class="btn btn-success" th:href="@{__${addpage}__}" th:text="#{button.book.add}"></a>
</div>
</div>
</body>


Loading…
Cancel
Save