Application.properties

server.port = 8090spring.jpa.hibernate.ddl-auto=createlogging.level.org.hibernate.SQL=DEBUGlogging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACEspring.datasource.url=jdbc:mysql://localhost:3306/booksspring.datasource.username=rootspring.datasource.password=qwerty

Messages.properties

name=Name:author=Author:date=Date:genre=Genre:rating=Rating:addBtn=Add

Index.html

 

<!DOCTYPE HTML>

<html xmlns:th="http://www.thymeleaf.org">

<head>

<title>Books</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

</head>

<body>

 

 

<form action="#" th:action="@{/index}" th:object="${book}" method="post">

<label for="name"><p th:text="#{name}"></p></label>

<input type="text" id="name" name="name" th:field="*{name}" required=""/>

<label for="author"><p th:text="#{author}"></p></label>

<input type="text" id="author" name="author" th:field="*{author}" required=""/>

<label for="date"><p th:text="#{date}"></p></label>

<input type="date" id="date" name="date" th:field="*{date}" required=""/>

<label for="genre"><p th:text="#{genre}"></p></label>

<input type="text" id="genre" name="genre" th:field="*{genre}" required=""/>

<label for="rating"><p th:text="#{rating}"></p></label>

<input type="number" id="rating" name="rating" th:field="*{rating}" required=""/>

<button id="addBtn" th:text="#{addBtn}"></button>

</form>

 

<p th:text="${textFromUrl}"></p>

 

<th:block th:each="book: ${books}">

<tr>

<td>

<p th:text="${book.name}"></p>

</td>

<td>

<p th:text="${book.author}"></p>

</td>

<td>

<p th:text="${#dates.format(book.date, 'mm/dd/yyyy')}"></p>

</td>

<td>

<p th:text="${book.genre}"></p>

</td>

<td>

<p th:text="${book.rating}"></p>

</td>

</tr>

<p>_______________</p>

</th:block>

 

</body>

</html>

 

 

Book.java

 

package com.avasilyev.sprboot.model; import org.springframework.format.annotation.DateTimeFormat; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import java.util.Date; @Entity publicclass Book { @Id@GeneratedValue(strategy= GenerationType.AUTO) privateInteger id; privateString name; privateString author; @DateTimeFormat(pattern="yyyy-mm-dd") privateDate date; privateString genre; private int rating; public Book(){}; public Book(String name, String author, Date date, String genre,int rating){ this. name = name; this. author = author; this. date = date; this. genre = genre; this. rating = rating;} publicString getName(){ return name;} public void setName(String name){ this. name = name;} publicString getAuthor(){ return author;} public void setAuthor(String author){ this. author = author;} publicDate getDate(){ return date;} public void setDate(Date date){ this. date = date;} publicString getGenre(){ return genre;} public void setGenre(String genre){ this. genre = genre;} public int getRating(){ return rating;} public void setRating(int rating){ this. rating = rating;}}

 

 

BookRepository.java

package com.avasilyev.sprboot.repository; import com.avasilyev.sprboot.model.Book; import org.springframework.data.repository.CrudRepository; publicinterface BookRepository extends CrudRepository<Book, Integer>{}

MainController.java

package com.avasilyev.sprboot.web; import com.avasilyev.sprboot.repository.BookRepository; import com.avasilyev.sprboot.model.Book; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype. *; import org.springframework.web.bind.annotation. *; import java.io. *; import java.net.URL; import java.util.Map; @Controller publicclass MainController { @Autowired private BookRepository bookRepository; @RequestMapping(value ="/index", method = RequestMethod.GET) publicString getCarsList(Map< String, Object > model){model.put("books", bookRepository.findAll());model.put("book", new Book());model.put("textFromUrl", getText()); return "index";} @RequestMapping(value ="/index", method = RequestMethod.POST) String hm(@ModelAttribute Book book){bookRepository.save(book); return "redirect:/index";} privateString getText(){ finalString urlStr ="https://example.com"; String content =""; try { StringBuilder contentBuilder = new StringBuilder(); try { URL url = newURL (urlStr); BufferedReader in = newBufferedReader (newInputStreamReader (url.openStream())); String inputLine; while ((inputLine = in.readLine())!= null)content+= inputLine;in.close();} catch (IOException e){content= e.getMessage();}} catch (Exception err){content= err.getMessage();} return content;}}

Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: