百里香叶表单提交:CRUD 操作更新无法正常工作.没有错误



制作Spring Boot + JPA + Thymeleaf表单提交项目。它只是为了学习目的。我想创建公司并更新它。

无法解决 CRUD 问题。

crud 操作"创建"工作正常,但"更新"不能,它只是再次创建一个新操作。请帮忙。

我的公司控制器

package ua.com.mmplus.promomanagement.controller;
import org.slf4j.Logger;
@Controller
public class CompanyController {
@Autowired
private CompanyService companyService;
private Logger logger = LoggerFactory.getLogger(this.getClass());
@RequestMapping(value = "/company/add", method = RequestMethod.GET)
public String companyForm(Model model){
model.addAttribute("company", new Company());
return "companyform";
}
@RequestMapping(path = "companylist", method = RequestMethod.POST)
public String companySubmit(Company company){
String info = String.format("Company submission: id = %d, company = %s" +
", email = %s", company.getId(), company.getCompanyName(),
company.getCompanyEmail());
logger.info(info);

companyService.save(company);
return "redirect:/";
}

@GetMapping("/companylist")
public String getCompanyList(Model model) {
	model.addAttribute("companies", companyService.getAll());
	return "companylist";
}

@GetMapping("company/edit/{id}")
public String updateCompany(@PathVariable(name = "id") Long id, Model model) {
	model.addAttribute("company", companyService.findById(id));
	return "companyform";
}
}

我的公司形式.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all"
href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
</head>
<body>
<h1>Company Form</h1>
<form action="#" th:action="@{/companylist}" th:object="${company}" method="post">
<p>New Company Name: <input type="text" th:field="*{companyName}" /></p>
<p>email: <input type="text" th:field="*{companyEmail}" /></p>
<p><input type="submit" value="SUBMIT"/><input type="reset" value="RESET"/></p>
</form>
<a href="home">У головне меню</a>
</body>
</html>

问题解决了! 要传递id,我必须添加到公司表单.html以下行:

**<input type="hidden" th:field="*{id}"/>**

所以它应该看起来像这样:

<form class="form horizontal" th:action="@{/eventform}" th:object="${event}" method="post">
<table>

<input type="hidden" th:field="*{id}"/>

<select class="form-control" th:field="*{promo}">
<option th:each="promo: ${promoList}"
th:value="${promo.getId()}"
th:utext="${promo.getPromoName().toString()}"
></option>
</select>
<select class="form-control" th:field="*{company}">
<option th:each="companyList : ${companies}"
th:value="${{companyList.getId()}}"
th:utext="${companyList.getCompanyName().toString()}"></option>
</select>
<select class="form-control" th:field="*{supermarket}">
<p>Вибір постачальника</p>
<option th:each="supermarket : ${supermarketList}"
th:value="${supermarket.getId()}"
th:utext="${supermarket.getSupermarketName().toString()}">
</option>
</select>
<p>Опис події: <input type="text" th:field="*{description}"/> </p>
</table>
<p><input type="submit" value="Додати подію"></p>
<a href="/">У головне меню</a>
</form>

最新更新