Answer the question
In order to leave comments, you need to log in
How to correctly read data from a web page and put it in the database?
I have an interface that inherits from the interface for working with the database provided by spring
import org.springframework.data.repository.CrudRepository;
public interface UserRepo extends CrudRepository<ClientOrder, Integer> {
}
public class AppUserDAO {
@Autowired
private UserRepo userRepository;
public void addUserAccount(ClientOrder clientOrder){
userRepository.save(clientOrder);
}
public Iterable<ClientOrder> findAllOrders(){
Iterable<ClientOrder> customers = userRepository.findAll();
return customers;
}
}
@Entity
@Table(name = "Web_client")
public class ClientOrder {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "email", nullable = false)
private String eMail;
@Column(name = "password", nullable = false)
private String password;
//...
}
@Controller
public class MainContoller {
private static Iterable<ClientOrder> orders;
private AppUserDAO userDAO;
@Value("${error.message}")
private String errorMessage;
@RequestMapping(value = {"/"}, method = RequestMethod.GET)
public String index(Model model){
return "index";
}
@RequestMapping(value = { "/personList" }, method = RequestMethod.GET)
public String personList(Model model) {
orders = userDAO.findAllOrders();
model.addAttribute("persons", orders);
return "personList";
}
@RequestMapping(value = { "/registerPerson" }, method = RequestMethod.POST)
public String savePerson(Model model, //
@ModelAttribute("personForm") ClientOrder personForm) {
String firstName = personForm.getFirstName();
String lastName = personForm.getLastName();
String email = personForm.geteMail();
String password = personForm.getPassword();
if (firstName != null && firstName.length() > 0 //
&& lastName != null && lastName.length() > 0) {
ClientOrder newPerson = new ClientOrder();
newPerson.setFirstName(firstName);
newPerson.setLastName(lastName);
newPerson.seteMail(email);
newPerson.setPassword(password);
userDAO.addUserAccount(newPerson);
return "redirect:/personList";
}
model.addAttribute("errorMessage", errorMessage);
return "addPerson";
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<input value="register" type="button" onclick="location.href=registerPerson" />
<input value="Log in" type="button" onclick="location.href='personList.html'" />
</body>
</html>
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Add Person</title>
</head>
<body>
<h1>Create a Person:</h1>
<!--
In Thymeleaf the equivalent of
JSP's ${pageContext.request.contextPath}/edit.html
would be @{/edit.html}
-->
<form th:action="@{/registerPerson}"
th:object="${personForm}" method="POST">
First Name:
<input type="text" th:field="*{firstName}" />
<br/>
Last Name:
<input type="text" th:field="*{lastName}" />
<br/>
email :
<input type="text" th:field="*{email}" />
<br/>
password:
<input type="text" th:field="*{password}" />
<br/>
<input type="submit" value="Create" />
</form>
<br/>
<!-- Check if errorMessage is not null and not empty -->
<div th:if="${errorMessage}" th:utext="${errorMessage}"
style="color:red;font-style:italic;">
...
</div>
</body>
</html>
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Person List</title>
</head>
<body>
<h1>Person List</h1>
<a href="addPerson">Add Person</a>
<br/><br/>
<div>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<tr th:each ="person : ${persons}">
<td th:utext="${person.getFirstName}">...</td>
<td th:utext="${person.getLastName}">...</td>
<td th:utext="${person.getEmail}">...</td>
</tr>
</table>
</div>
</body>
</html>>
Answer the question
In order to leave comments, you need to log in
Hello!
The first thing that immediately catches your eye -
public interface UserRepo extends CrudRepository<ClientOrder, Integer> {
}
public class ClientOrder {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Long id;
public String savePerson(Model model,
@ModelAttribute("personForm") ClientOrder personForm) {
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question