Results 1 to 4 of 4
- 02-16-2017, 04:41 AM #1
Senior Member
- Join Date
- Sep 2016
- Posts
- 108
- Rep Power
- 0
Help retrieving values from fields
Hello, So I have been working through the first few chapters of spring boot in action. I have a up and running spring boot application that has an index page which I click a link and can choose between three different pages that load up a list of Books, People, Goats from a MySQL server running locally. The readingList and Book model came from the book and then all I did was make more except with different names.
However the people and goats page the html is broken. It does not show the item once it is added to the table. I know it is added because I have a MySQL cmdline open and I check before and after to confirm. So the problem is somewhere with the html but I am not sure. It is a copy of the bookList html which is working. I only changed the names from book to person or from book to goat and changed things like title or isbn to name or breed or whatever fit, you'll see.
First I'll show the working readingList.html wich does work properly. Then I will add just the goatList.html to compare.
Java Code:<!DOCTYPE html> <html> <head> <!-- the meta tag needs to be like one below, not one from examples. --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Reading List</title> <link rel="stylesheet" th:href="@{/style.css}"></link> </head> <body> <h2>YOUR READING LIST</h2> <div th:unless="${#lists.isEmpty(books)}"> <dl th:each="book : ${books}"> <dt class="bookHeadline"> <span th:test="${book.title}">Title</span> by <span th:text="${book.author}">Author</span> (ISBN: <span th:text="${book.isbn}">ISBN</span>) </dt> <dd class="bookDescription"> <span th:if="${book.description}" th:text="${book.description}">Description</span> <span th:if="${book.description eq null}">No description available</span> </dd> </dl> </div> <div th:if="${#lists.isEmpty(books)}"> <p>You have no books in your book list</p> </div> <hr/> <h3>Add a book</h3> <form method="POST"> <label for="title">Title:</label> <input type="text" name="title" size="50"></input><br/> <label for="author">Author:</label> <input type="text" name="author" size="50"></input><br/> <label for="isbn">ISBN:</label> <input type="text" name="isbn" size="15"></input><br/> <label for="description">Description:</label><br/> <textarea name="description" cols="80" rows="5"> </textarea><br/> <input type="submit"></input> </form> </body> </html>
goatList.html
Java Code:<!DOCTYPE html> <html> <head> <!-- the meta tag needs to be like one below, not one from examples. --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Goat List</title> <link rel="stylesheet" th:href="@{/style.css}"></link> </head> <body> <h2>YOUR GOAT LIST</h2> <div th:unless="${#lists.isEmpty(goats)}"> <dl th:each="goat : ${goats}"> <dt class="goatHeadline"> <span th:test="${goat.name}">Name</span> <span th:text="${goat.breed}">Breed</span> <span th:text="${goat.age}">Age</span> </dt> <dd class="goatDescription"> <span th:if="${goat.description}" th:text="${goat.description}">Description</span> <span th:if="${goat.description eq null}">No description available</span> </dd> </dl> </div> <div th:if="${#lists.isEmpty(goats)}"> <p>You have no goats in your goat list</p> </div> <hr/> <h3>Add a Goat</h3> <form method="POST"> <label for="name">Name:</label> <input type="text" name="name" size="50"></input><br/> <label for="breed">Breed:</label> <input type="text" name="breed" size="50"></input><br/> <label for="age">Age:</label> <input type="text" name="age" size="15"></input><br/> <label for="description">Description:</label><br/> <textarea name="description" cols="80" rows="5"> </textarea><br/> <input type="submit"></input> </form> </body> </html>
- 02-16-2017, 05:33 AM #2
Senior Member
- Join Date
- Sep 2016
- Posts
- 108
- Rep Power
- 0
Re: Help retrieving values from fields
okay so I somehow got the goat page working. I changed in my goatRepository the findBy method. now it is the same as in the readinglistRepository and it works. I did the exact same thing with the personRepository and it did not work lol.
Java Code:<!DOCTYPE html> <html> <head class="personHead"> <!-- the meta tag needs to be like one below, not one from examples. --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Reading List</title> <link rel="stylesheet" th:href="@{/style.css}"></link> </head> <body class="personBody"> <h2>PERSON LIST</h2> <div th:unless="${#lists.isEmpty(persons)}"> <dl th:each="person : ${persons}"> <dt class="personHeadline"> Name: <span th:text="${person.name}">Name</span> Gender: <span th:text="${person.gender}">Gender</span> Age: <span th:text="${person.age}">Age</span> Occupation: <span th:text="${person.occupation}">Occupation</span> </dt> <dd class="personDescription"> Description: <span th:if="${person.description}" th:text="${person.description}">Description</span> <span th:if="${person.description eq null}">No description available</span> Hobbies: <span th:if="${person.hobbies}" th:text="${person.hobbies}">Hobbies</span> <span th:if="${person.hobbies eq null}">No hobbies available</span> </dd> </dl> </div> <div th:if="${#lists.isEmpty(books)}"> <p>You have no People in your list</p> </div> <hr/> <h3>Add a book</h3> <form method="POST"> <label for="name">Name:</label> <input type="text" name="name" size="50"></input><br/> <label for="gender">Gender:</label> <input type="text" name="gender" size="50"></input><br/> <label for="age">Age:</label> <input type="text" name="age" size="15"></input><br/> <label for="occupation">Occupation:</label> <input type="text" name="occupation" size="15"></input><br/> <label for="description">Description:</label><br/> <textarea name="description" cols="80" rows="5"> </textarea><br/> <label for="hobbies">Hobbies:</label><br/> <textarea name="hobbies" cols="80" rows="5"> </textarea><br/> <input type="submit"></input> </form> </body> </html>
Java Code:package readinglist; import org.springframework.beans.factory.annotation.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; import org.springframework.ui.*; import java.util.List; @Controller @RequestMapping("/person") public class PersonController { private PersonRepository personRepo; @Autowired public PersonController(PersonRepository personListRepo) { this.personRepo = personListRepo; } @RequestMapping(value="/{reader}", method=RequestMethod.GET) public String readerPersons(@PathVariable("reader") String reader, Model model) { List<Person> personList = personRepo.findByReader(reader); if (personList != null) { model.addAttribute("person", personList); } return "personList"; } @RequestMapping(value="/{reader}", method=RequestMethod.POST) public String addToPersonList(@PathVariable("reader") String reader, Person person) { person.setReader(reader); personRepo.save(person); return"redirect:/person/{reader}"; } }
Java Code:package readinglist; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; public interface PersonRepository extends JpaRepository<Person, Long> { List<Person> findByReader(String reader); }
controllers
Java Code:package readinglist; import org.springframework.beans.factory.annotation.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; import org.springframework.ui.*; import java.util.List; @Controller @RequestMapping("/readinglist") public class ReadingListController { private ReadingListRepository readingListRepository; @Autowired public ReadingListController(ReadingListRepository readingListRepository) { this.readingListRepository = readingListRepository; } @RequestMapping(value="/{reader}", method=RequestMethod.GET) public String readerBooks(@PathVariable("reader") String reader, Model model) { List<Book> readingList = readingListRepository.findByReader(reader); if (readingList != null) { model.addAttribute("books", readingList); } return "readingList"; } @RequestMapping(value="/{reader}", method=RequestMethod.POST) public String addToReadingList(@PathVariable("reader") String reader, Book book) { book.setReader(reader); readingListRepository.save(book); return "redirect:/readinglist/{reader}"; } }
Java Code:package readinglist; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; public interface ReadingListRepository extends JpaRepository<Book, Long> { List<Book> findByReader(String reader); }
- 12-13-2017, 04:07 PM #3
Senior Member
- Join Date
- Apr 2015
- Posts
- 288
- Rep Power
- 4
Re: Help retrieving values from fields
Hello. I did not used JPA and jparepository in practice. Indeed I have one question as I need to consume the rest-services that manipulates db with repository that implements jparepository. How the db is created - if automatically - why that sever code has no persistence.xml with option to "create" db according the jpa annotation.
And if there is such methods as:
Java Code:@Override public void delete(int id) { sampleRepository.delete(sampleRepository.findById(id).get()); } @Override public void save(Class entity) { SampleRepository.saveAndFlush(entity);}
And what about saving or updating entities -- is that simple schema?
According to it - https://docs.spring.io/spring-data/j...ositories.html - no entitymanager objects as the substitution for hibernatesessions and @transactional - for transactions?Last edited by ark; 12-13-2017 at 04:21 PM.
- 12-13-2017, 08:20 PM #4
Senior Member
- Join Date
- Apr 2015
- Posts
- 288
- Rep Power
- 4
Re: Help retrieving values from fields
Yes I understand that persistence.xml is not nessesary for with JPA annotations. But how tables and where is created even update mode is choosen in Spring Boot application properties.
And is JPA repositories self-reflected according to its name - are not error-prone?
Similar Threads
-
how to add rows dynamically and retrieving values in jsp
By Gopinathsachin in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 11-27-2011, 03:06 PM -
Problem in retrieving values from Oracle 10g database
By techsing14 in forum JavaServer Pages (JSP) and JSTLReplies: 9Last Post: 04-13-2011, 01:50 AM -
Java 2D array get values from text fields
By raverz1tildawn in forum Java 2DReplies: 3Last Post: 01-08-2011, 12:56 AM -
Populating the values in text fields
By Inaam in forum Web FrameworksReplies: 2Last Post: 07-26-2010, 08:55 AM -
declaring fields without assigning values to them
By diggitydoggz in forum New To JavaReplies: 12Last Post: 01-03-2009, 08:22 PM
Bookmarks