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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. Doing it directly in the html file would be my preferred solution, but when I do this: <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> or this: <form th:action="@{/distance/{pathParam}(pathParam=${param.dist})}">` <p>Distance: <input type="text" th:field="*{param.dist}" /></p> <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p> </form> I still get error
The error I get is: Neither BindingResult nor plain target object for bean name 'param' available as request attribute
Based on thymeleaf.org/doc/tutorials/3.0/…. u should be able to get the query params in param object.

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.