0

My form is based on Thymeleaf, I am not using tables because it has many fields.

In the form I have 3 buttons, one is a submit and the others are links. With a link button I want to make a query using a parameter that sends a string to the controller.

I want to get the string that was passed in the URL to the controller and receive the data with an @RequestParam annotation.

I have no error messages, what happens is that the string reaches the controller empty.

This is my link button:

<a class="btn btn-success text-white btn-sm mr-3 " style="border-radius: 16px;" th:href="@{/config/item/items/select(productId=${item.productId})}">
        Search  
    </a>

This is my text field where the user places the query:

    <div class="col-2 form-group"
        th:classappend="${#fields.hasErrors('item.productId')}? 'has-error':''">
        <label for="productId"><strong>Item id </strong></label> <input
            type="text" class="form-control" id="productId" name="productId"
            th:field="${item.productId}">
    </div>

And all this goes inside my form:

        <form th:action="@{/config/item/items/save}" method="get"
            th:model="${item}">

I have used these two but with no results:

th:href="@{'/config/item/items/select'+${item.productId}}"

th:href="@{|/config/item/items/select${item.productId}|}"

I have reviewed the documentation provided by Thymeleaf at: https://www.thymeleaf.org/doc/articles/standardurlsyntax.html, in chapter number nine. And I have also seen tutorials but it doesn't work.

1 Answers1

1

If you want to use the @RequestParam annotation you need to add a parameter in your link. Your code should be something like:

  th:href="@{select(productId=${item.productId})"

(According you added the object item to your template)

In your controller "select" you have to use the annotation @RequestParam to retrieve the info:

 @GetMapping (path = "/select")
 public String list(Model model,
        @RequestParam(name = "productId") int productId) { 
 // rest of the code
 }
Xtof
  • 415
  • 5
  • 11