A
A
Amantoss2020-09-05 10:49:32
Spring
Amantoss, 2020-09-05 10:49:32

How to transfer a user?

I have a task to make a new user (I use Spring Boot) and first I did it through a link to the page for creating a new user, through a get request, the controller catches and passes the attribute of the new empty user to the Model, and the fields are already filled in the form, using Thymeleaf and is passed to the post controller, but when I connected Bootstrap there is no redirection, but just a tab opens without loading (apparently here JS) and I don’t understand how to pass an empty User, because there is no get request, everything is dynamic, I just can’t figure out how to implement it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2020-09-05
@azerphoenix

Hello!
The registration form must be sent not with a GET request, but with a POST request.
Given that you are using thymeleaf, you can bind the form itself to an object and get a ready-made object right away. Pay attention to the tag th:object
https://o7planning.org/ru/12385/using-thymeleaf-th...
Here is a good example:
https://stackoverflow.com/questions/32045271/how-t...

<form th:action="@{/the-action-url}" method="post"
    th:object="${myEntity}">

    <div class="modal-body">
        <div class="form-group">
            <label for="name">Name</label> <input type="text"
                class="form-control" id="name" th:field="*{name}"> </input>
        </div>

        <div class="form-group">
            <label for="description">Description</label> <input type="text"
                class="form-control" id="description"
                th:field="*{description}"> </input>
        </div>
    </div>
</form>

Well, or in the controller you just get a link to the User object user.
Further, through setters, set the values ​​obtained from @ModelAttribute and save.
I'll give an example:
@PostMapping("/register")
public String register(
@ModelAttribute("username") String username,
@ModelAttribute("password") String password,
User user
) {
user.setUsername(username);
user.setPassword(password);
userRepository.save(user);
return "index";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question