0

I'm trying to create controller which will be responsible for deleting specific displayed users by sending their id in request that will be cougth by method and proceed. So far I wrote something like this:

@RequestMapping("delete/{user.id}")
public String deleteUser(@PathVariable("user.id") String userId)
{
    userRepository.delete(Long.parseLong(userId));
    return "panel";
}

And I also created a dinamic table in my thymyleaf template that displays all users.

<tr th:each="user : ${userList}">
    <td th:text="${user.firstname}"></td>
    <td th:text="${user.lastname}"></td>
    <td th:text="${user.email}"></td>
    <td th:text="${user.birthdate}"></td>
    <td th:text="${user.password}"></td>
    <td><a href="delete/${user.id}.html">Delete</a></td>
    <td><a href="#">Edit</a></td>
</tr>

Unfortunatelly "delete/${user.id}.html" request doesn't work. Any sugestions?

Thank you in advance.

1 Answer 1

1

You have not described the error, if any, that you received. I think you may have multiple issues. Start by removing the ".html" from the URL. Having it means the request will not match the path you have in the RequestMapping annotation.

I also recommend changing the userId parameter to a long. Spring will take care of the parsing.

public String deleteUser(@PathVariable("user.id") long userId)

You should specify the expected HTTP method used:

@RequestMapping(method=RequestMethod.GET, path="delete/{user.id}")

You don't really need to use "user.id" as the name of the path parameter. You can just use "id".

@RequestMapping(method=RequestMethod.GET, path="delete/{id}")
public String deleteUser(@PathVariable("id") long userId)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for response. I did those recommended changes, but still got error as below. I have Whitelabel Error Page and message like this: There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "${user"
The URL embedded in your Thymeleaf template is malformed. If you hover the link in your browser, you will find that the URL contains delete/${user.id} and not delete/1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.