gpt4 book ai didi

java - PostMapping 问题 - GetMapping 方法 - 链接中循环。 SpringBoot

转载 作者:行者123 更新时间:2023-12-02 10:37:41 28 4
gpt4 key购买 nike

我在将数据发送到我的一张表时遇到问题。下面你可以看到我的方法:一个显示带有表单的模板,第二个应该添加此操作。

@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

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

最佳答案

您不必通过'。 spring 表达式语言也将不带 '

尝试像下面这样删除。

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

引用thymeleaf-construct-url-with-variable

关于java - PostMapping 问题 - GetMapping 方法 - 链接中循环。 SpringBoot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53157522/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com