0

I am new to Thymeleaf, and I have an issue with dynamically creating URLs using forms.

I have a simple spring boot app that stores objects in a database, and I can query the database using simple urls. For instance, the url /distance/0.3 would return objects whose distance attribute is lower than or equal to 0.3. These urls work perfectly when I edit them in the browser.

I would like users to be able to set these search values themselves. I tried to create a simple form to create the above url with user inputs, but I am getting the following error:

    Neither BindingResult nor plain target object for bean name 'dist' available as request attribute

I have tried with this in the html document:

<form th:action="@{/distance/{pathParam}(pathParam=${dist}}">`
    <p>Distance: <input type="text" th:field="*{dist}" /></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

and trying various answers from this discussion, but with no luck.

I have also tried to use the controller as suggested here, with this in the controller:

@GetMapping("/distance/search/")
public String userSetDistance(@RequestParam("dist") String dist) {

    return "redirect:/distance/" + dist;
}

and this in the html file:

<form th:action="@{/distance/search/}">
    <p>Distance: <input type="text" th:field="*{dist}" /></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

But this did not work either.

Could you please help me with this? The idea is simple but I cannot get something that works... thank you!

UPDATE

Based on the below answer from MohamedSanaulla, I decided to use the controller to do this, created a "forms" object with the required fields and edited my code as follows:

<form action="#" th:action="@{/distance/search}" th:object="${param}" method="post">`
    <p>Distance: <input type="text" th:field="*{dist}"/></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>


@PostMapping("/distance/search")
 public String userGetClose(@ModelAttribute ("param") Forms form) {
    String distance = String.valueOf(form.getDist());
 return "redirect:/distance/" + distance;
 }

Ideally I wanted to create and call the url directly from the html page, to avoid going back to the controller just to create the url, but this works fine.

Guillaume
  • 357
  • 4
  • 10

1 Answers1

1

You have to either use ModelMap or Model in your controller and then use addAttribute to set the dist:

public String controllerMethod(@RequestParam("dist") String dist, 
  Model model){
  model.addAttribute("dist", dist);
  return "viewName";
}

Or Thymeleaf provides context object to get query params like ${param.dist} directly in your HTML

Update:

Just saw that there is a redirect: in the controller. So the dist is no longer available in the request param and hence ${param.dist} will not work. The easier solution is to use ModelMap and put the dist as part of the view model.

MohamedSanaulla
  • 6,112
  • 5
  • 28
  • 45
  • Thank you for your answer. Doing it directly in the html file would be my preferred solution, but when I do this:
    `

    Distance:

    or this:
    `

    Distance:

    I still get error
    – Guillaume Dec 08 '19 at 09:21
  • The error I get is: Neither BindingResult nor plain target object for bean name 'param' available as request attribute – Guillaume Dec 08 '19 at 09:24
  • 1
    Based on https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#web-context-namespaces-for-requestsession-attributes-etc. u should be able to get the query params in param object. – MohamedSanaulla Dec 08 '19 at 09:34