Take the value of a dynamic span element

Asked

Viewed 312 times

1

I need to get an id value of a dynamic span and I’m not getting it. What I’m doing wrong?

Error:

There was an Unexpected error (type=Bad Request, status=400).
Required Long Parameter 'id' is not present

View:

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<head>
<meta charset="UTF-8"></meta>
<title>CRUD</title>
<link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <h1>Edit or Delete User</h1>

    <table class="table table-hover">
        <thead>
            <tr>
                <th>Id</th>
                <th>User</th>
            </tr>
        </thead>

        <td><span th:text=${user.id} name="id"></span></td>
        <td><span th:text=${user.name} name="name"></span></td>

    </table>


    <form action="../update" method="post">

        <div id="userEdit"></div>


        <div>
            <td><label for="userpassword">Password:</label> <input
                type="text" id="userpassword" name="user_password"></td>
        </div>
        <div class="button">
            <button type="submit">Update</button>
        </div>

    </form>

    <hr>

    <form action="deleteUser" method="post">
        <div class="button">
            <button type="submit">Delete</button>
    </form>
</body>
</html>

Method:

    @RequestMapping(value = "update", method = RequestMethod.POST)
public ModelAndView updateUser(@RequestParam("id") Long id,
        @RequestParam("user_password") String newPassword) {

    User user = dao.find(id);
    User newUser = dao.find(user.getId());
    newUser.setPassword(newPassword);
    dao.save(newUser);

    return new ModelAndView("redirect:crud");
}

I’m wearing spring and Thymeleaf.

1 answer

3


The Ubmit button inside a <form> sends only the data that is inside that <form>. Therefore, only the countryside user_password will be sent.

The solution is to use this:

    <form action="../update" method="post">

        <div id="userEdit"></div>

        <div>
            <label for="userpassword">Password:</label>
            <input type="text" id="userpassword" name="user_password" />
            <input type="hidden" value="${user.id}" name="id" />
        </div>
        <div class="button">
            <button type="submit">Update</button>
        </div>
    </form>

    <hr>

    <form action="deleteUser" method="post">
        <div class="button">
            <input type="hidden" value="${user.id}" name="id" />
            <button type="submit">Delete</button>
        </div>
    </form>

That is, the solution is to put a <input type="hidden" value="${user.id}" name="id" /> in each <form>.

Besides, I took the <td> and the </td> of the first <form>, because it doesn’t make sense there and I added the </div> that was missing within the second <form>.

Also, I recommend using the method="delete" in the second <form> and then use the method = RequestMethod.DELETE on the Java side. The reason is that the DELETE method is the HTTP method that was created to delete things.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.