Thymeleaf表单提交:CRUD操作UPDATE无法正常工作。没有错误

时间:2018-05-01 09:09:25

标签: java spring-boot jpa thymeleaf crud

制作Spring Boot + JPA + Thymeleaf表单提交项目。它仅用于研究目的。我想创建公司并更新它。

无法解决CRUD问题。

Crud操作'create'正常工作,但'update'没有,它只是再次创建一个新的。请帮忙。

我的公司控制器

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

我的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>

1 个答案:

答案 0 :(得分:0)

问题解决了! 要传递ID,我必须将此行添加到companyform.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>