PostMapping问题-GetMapping方法-链接循环。弹簧靴

时间:2018-11-05 15:39:30

标签: java spring-boot spring-data-jpa

我无法将数据发送到我的一张桌子。 您可以在下面看到我的方法:一种显示带有表单的模板,另一种应添加此操作。

@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;
    }

这是我在服务中的方法:

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);

    }

和我的模板: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>

主要问题是:当我尝试执行addAction时,结果是:

  

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

存在某种循环。我究竟做错了什么?感谢您的时间!

1 个答案:

答案 0 :(得分:1)

您不必通过'。春季表达语言也将不使用'

尝试如下所示删除。

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

引用thymeleaf-construct-url-with-variable