D
D
Denis Kuznetsov2019-06-08 22:54:34
Java
Denis Kuznetsov, 2019-06-08 22:54:34

How to create a scrollbar on thymeleaf that returns a string?

Hello, if I want to fill in some fields on the object mapped to the page, then it will look something like this:

<form th:action="@{'/plantEdit/'+${boxId}}"
      th:object="${plant}" method="POST">
    <input th:type="hidden" name="id" th:field="${plant.id}" />
    Plant name:
    <input type="text" th:field="*{plantName}" />
    <br/>
    <input type="submit" value="Create" />
    <a href="#" th:href="@{/}" role="button">Cancel</a>
</form>

i.e. during the action on the page, the controller responsible for post will be called and submit the value that the user will enter, moreover, when this page was just called (get) , then an object was assigned to the "plant" parameter and then this object was passed to the function responsible for post.
But I need, for example, to raise data from the database of supported plants (only names are acceptable), after these names must somehow be transferred to the page in the scroll bar, so that the user already selects a plant from them and this string (name) is transmitted to the controller when submitting , by this name from the database of supported plants, some more data will be pulled up that is needed when creating an object that the user will put in his box.
here are my controllers :
@RequestMapping(value = {"/plantEdit/{boxId}", "/plantEdit/{boxId}/{id}"}, method = RequestMethod.GET)
    public String growBoxesEditForm(Model model,
                                    @PathVariable(name = "boxId") Long boxId,
                                    @PathVariable(required = false, name = "id") Long id) throws ResourceNotFoundException {

        logger.info("now we observe plant with id : " + id);

        if(id != null){
            model.addAttribute("plant", plantService.findById(id));
        }else {
            model.addAttribute("plant", new Plant());
        }

        model.addAttribute("boxId", boxId);

        return "plantEdit";
    }

    @RequestMapping(value = "/plantEdit/{boxId}", method = RequestMethod.POST)
    public String growBoxEditForm(Model model, Plant plant,
                                  @PathVariable(name = "boxId") Long boxId) throws ResourceNotFoundException {

        GrowBox box = growBoxService.findById(boxId);
        plant.setResponsibleGrowBox(box);

        plantService.savePlant(plant);

        model.addAttribute("plants", plantService.findByBoxId(boxId));
        return "showPlants";
    }

the question is how to write a scroll bar and process the selected value in the controller, thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2019-06-09
@DennisKingsman

To be honest, I didn't quite understand what you mean by "scrollbar" in this context...
Based on this text:
Let's say you want to create a dropdown list with a scrollbar where the user can select data.
To implement it, you need to do the following:
1) implement a method that returns a List or an array of strings with plant names. (in Repository).
2) In a GET request, when opening a page, model.addAttribute("plants", repo.getPlants())pass this information through Model ( ).
3) then it remains to wrap the whole thing nicely in a dropdown list.
https://getbootstrap.com/docs/4.3/components/dropdowns/
You can store possible plants as in a database (if frequent additions and deletions of varieties are expected) or stored as an Enum.
depends on what you choose. In a controller method, you can take an object and save it, you can take an enum and save it, you can take a String and use String.valueOf(myString) to save the enum, and so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question