D
D
Dmitry Volokitin2020-07-21 15:14:02
Java
Dmitry Volokitin, 2020-07-21 15:14:02

No data is added to table when input is clicked (java). How to fix it?

Previously, I had two html pages: one displays users, the second creates. Both worked, but when you clicked on the input, the page was simply updated, and if you go to the page with users in the url, the data was added.
I combined these two pages, the table now looks like this: the first line is the name of the columns, the second is adding data, the third and subsequent are outputting data. BUT! Now, when you click on the input, not only does the page not reload, but it also does not add data. I know that I cheated with the mapping, but I don’t know how to do it correctly. I turned the "spring in action", turned the Russian-language Internet, I did not find the answer. Explain to the jun what the problem is and how to do it correctly

Vyushka:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title>Список сотрудников</title>
    <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}"/>
</head>
<body>
<h1>Список</h1>
<br/><br/>
<div> <!--Пробная страничка, для объединения user.html и user_created.html-->
    <table border="1">
        <tr>
            <th>ID</th>
            <th>Имя</th>
            <th>Фамилия</th>
            <th>Почта</th>
            <th>Номер тел.</th>
        </tr>
        <tr form th:action="@{/}"
            th:object="${addUser}" method="POST">
            <td><input/></td>
            <td><input type="text" th:field="*{lastName}" /></td>
            <td><input type="text" th:field="*{firstName}" /></td>
            <td><input type="text" th:field="*{email}" /></td>
            <td><input type="text" th:field="*{number}" /></td>
            <input type="submit" value="add" />
            </form>
        </tr>
        <tr th:each ="user : ${user}">
            <td th:utext="${user.id}">...</td>
            <td th:utext="${user.firstName}">...</td>
            <td th:utext="${user.lastName}">...</td>
            <td th:utext="${user.email}">...</td>
            <td th:utext="${user.number}">...</td>
        </tr>
    </table>
</div>
</body>
</html>


Controller:
package ru.noorsoft.javaeducation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class UsersController {

    @Autowired
    private UserRepository us;

    public UsersController(UserRepository us) {
        this.us = us; //объект репозитория
        us.save(new User("Dmitry", "Volokitin", "[email protected]", "89005910999"));//добавление
        //пробного пользователя, для отображения в таблице
    }
AddUser addUsers = new AddUser();
    @GetMapping("/")
    public String greeting(Model model, @ModelAttribute("addUser") AddUser addUser) {

        model.addAttribute("user", us.findAll());
        model.addAttribute("addUser", addUser);

        String firstName = addUser.getFirstName();
        String lastName = addUser.getLastName();
        String email = addUser.getEmail();
        String number = addUser.getNumber();

//функция проверки, заполнены ли ячейки с именем и фамилией
        if (firstName != null && firstName.length() > 0 //
                && lastName != null && lastName.length() > 0) {
            User newUser = new User(firstName, lastName, email, number);
            us.save(newUser);

            return "redirect:users";
        }
        model.addAttribute("errorMessage", errorMessage);

        return "users";
    }

    @Value("${welcome.message}")
    private String message;

    @Value("${error.message}")
    private String errorMessage;
}


https://github.com/chpokerstyle/java-spring-master/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2020-07-21
@Dima_gogi_ya

<tr form th:action="@{/}"
            th:object="${addUser}" method="POST">
            <td><input/></td>
            <td><input type="text" th:field="*{lastName}" /></td>
            <td><input type="text" th:field="*{firstName}" /></td>
            <td><input type="text" th:field="*{email}" /></td>
            <td><input type="text" th:field="*{number}" /></td>
            <input type="submit" value="add" />
            </form>

Well, first look at this piece of code. You have something wrong there. the form tag is nested inside the tr tag
<tr form th:action="@{/}"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question