D
D
Denis Kuznetsov2019-05-20 19:35:05
Java
Denis Kuznetsov, 2019-05-20 19:35:05

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> {
}

there is a Dao class for working with data
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;
    }
}

there is the entity itself with getters and setters (just in case, I will also indicate it)
@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;
//...
}

and there is a controller for working with pages
@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";
    }
}

which maps the initial page with index.html
<!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>

which links to 2 other pages with which its controllers are mapped:
registerPeson:
<!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>

and person list:
<!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>>

but when you try to open any of these links, nothing comes out, although everything seems to be mapped correctly and the context path is correctly specified, please help me figure it out, thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2019-05-21
@DennisKingsman

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;

Please note that you created the Long type, and use Integer
Accordingly, it should be like this
+ this is an interface, it is not necessary to write public
+ I hope that there is a constructor for the ClientOrder entity. Simply, I didn’t see this in the snippet, I decided to clarify. If not, then create:
In theory, here you need to uncomment the line
public String savePerson(Model model,
                             @ModelAttribute("personForm") ClientOrder personForm) {

It is better to check for emptiness not like this:
but like this:
since you have a string

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question