【问题标题】:In Spring boot and thymeleaf dropdown select option, I have = the server responded with a status of 404 () thymeleaf select option + oneTomany form在 Spring boot 和 thymeleaf 下拉选择选项中,我有 = 服务器响应状态为 404 () thymeleaf 选择选项 + oneTomany 形式
【发布时间】:2019-08-09 09:00:07
【问题描述】:

我是 thymeleaf 的新手,当我保存表“Pret”(添加)的新记录时遇到此错误:404。在班级表格中,我可以选择“Livre”的标题和“Lecteur”的名称。然后我的 addPret.html 被设计为。

<form th:action="@{SavePret}" method="post" 
             th:object="${pretFormulaire}">
     <select th:field="*{livre}">
          <option th:each="livre:${listLivres}"
                        th:value="${livre}"
                        th:text="${livre.titre}">   
          </option>
     </select> 
     <select th:field="*{lecteur}">
        <option th:each="lecteur:${listLecteurs}" 
                 th:value="${lecteur}" th:text="${lecteur.nom}">  
        </option>
    </select>
   <button type="submit" >Save</button>
</form>

这里是控制器第一个方法签名:

@RequestMapping(value = "/form", method = RequestMethod.GET)
public String formPret(Model model) {
    Pret pretFormulaire=new Pret();
    model.addAttribute("pretFormulaire", pretFormulaire);
    List<Lecteur> listLecteurs=lecteurRepository.findAll();
    List<Livre> listLivres=livreRepository.findAll();
    model.addAttribute("listLecteurs", listLecteurs);
    model.addAttribute("listLivres", listLivres);
    return "Form";
}

savePret的控制器方法是这样的:

@PostMapping(value = "/savePret")
    public String savePret2(@Valid @ModelAttribute("pret") 
              Pret pret, BindingResult bindingResult, 
               Long livre_id, Long lecteur_id){

        Livre livre=livreRepository.findOne(livre_id);
        Lecteur lecteur=lecteurRepository.findOne(lecteur_id);
        livre.setNbFoisPret(livre.getNbFoisPret()+1);
        livre.setDisponible(livre.getDisponible()+"Non");
        pret.setLecteur(lecteur);
        pret.setLivre(livre);
        pret.setDatePret(new Date());
        pretRepository.save(pret);

        return "redirect:Index";
    }

有人可以帮忙吗?

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:
    <form th:action="@{SavePret}" method="post" 
    

    应该是

    <form th:action="@{/savePret}" method="post" 
    

    【讨论】:

    • 谢谢!但我仍然有 = [出现意外错误 (type=Not Found, status=404)。]
    • 有没有人愿意帮助我!
    • 您的视图解析器必须解析名为“Index”的视图,因为您返回了“redirect:Index”;检查@RequestMapping("/Index") 或拼写错误。
    • 问题是“选择选项”在“字符串”类型而不是“对象”类型下返回。如何促进这种转变?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2018-02-03
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    相关资源
    最近更新 更多