0

I have problems with sending data to one of my tables. Below you can see my methods: one that shows the template with form, and the second that shoud add this action.

@GetMapping("/addaction/{id}")
    public String addAction(Model model, @PathVariable("id") int id ) {

        Optional<PlantEntity> plantEntity = plantService.getPlantById(id);
        if (plantEntity.isPresent()) {
            model.addAttribute("plant", plantEntity.get());
        }



        return "addaction";
    }

    @PostMapping("/addaction/{id}")
    public String addAction(@ModelAttribute ActionForm actionForm,
                           @PathVariable("id") int plantId) {

        if(!userService.isLogin()) {
            return "redirect:/";
        }
        actionService.addAction(actionForm, plantId);
        return "redirect:/plant/"+plantId;
    }

Here is my method in Service:

public void addAction (ActionForm actionForm, int plantId) {

        PlantEntity plantEntity = new PlantEntity();
        plantEntity.setId(plantId);

        ActionEntity act = new ActionEntity();

        act.setName(actionForm.getName());
        act.setDescription(actionForm.getDescription());
        act.setPlant(plantEntity);
        act.setUser(userService.getUserData());
        act.setMonth(actionForm.getMonth());

        actionRepository.save(act);

    }

and my template: addaction.html

<form method="post" action="'/addaction/'+${plant.getId()}"
                    th:object="${actionForm}">

                    <div class="form-group">
                        <label for="exampleInputEmail1">Name of activity</label> <input
                            type="text" th:field="*{name}" class="form-control"
                            id="exampleFormControlInput1" aria-describedby="emailHelp"
                            placeholder="Name your work">
                    </div>

                    <div class="form-group">
                        <label for="exampleFormControlTextarea1">What you gonna
                            do?</label>
                        <textarea class="form-control" th:field="*{description}"
                            id="exampleFormControlTextarea1" rows="4"></textarea>
                    </div>


                    <div class="input-group mb-3">
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="inputGroupSelect01">Month of activity</label>
                        </div>
                        <select class="custom-select" th:field="*{month}"
                            id="inputGroupSelect01">
                            <option value="1">January</option>
                            <option value="2">February</option>
                            <option value="3">March</option>
                            <option value="4">April</option>
                            <option value="5">May</option>
                            <option value="6">June</option>
                            <option value="7">July</option>
                            <option value="8">August</option>
                            <option value="9">September</option>
                            <option value="10">October</option>
                            <option value="11">November</option>
                            <option value="12">December</option>
                        </select>
                    </div>

                    <button type="submit" class="btn btn-dark">Action!</button>

                </form>

The main problem is: when I try to addAction the result is:

http://localhost:8080/addaction/'/addaction/'+$%7Bplant.getId()%7D

There is some kind of loop. What am I doing wrong? Thank you for your time!

danny
  • 4,337
  • 3
  • 9
  • 10

1 Answers1

1

You don't have to pass '. spring expression language will take without ' also.

Try removing like below.

action="/addaction/${plant.getId()}"

Refer thymeleaf-construct-url-with-variable

Alien
  • 15,141
  • 6
  • 37
  • 57
  • Thank you! This case was a little more complicated but your link was very helpful :) It works with: th:action="@{/addaction/{id}(id=${plant.getId()})}" – danny Nov 06 '18 at 11:05