我在Thymeleaf中创建了一个表单,它从用户处获取数据。它看起来像这样:Thymleaf表单提交结果404?
\t <h1>Form</h1>
<form action="#" th:action="@{/addArticle}" th:object="${article}" method="post">
\t <p>Id: <input type="text" th:field="*{id}" /></p>
<p>Message: <input type="text" th:field="*{title}" /></p>
<p>Message: <input type="text" th:field="*{authors}" /></p>
<p>Message: <input type="text" th:field="*{genre}" /></p>
<p>Message: <input type="text" th:field="*{content}" /></p>
<p>Message: <input type="text" th:field="*{date}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
的问题是,每当我提出这个形式,它不保存到数据库蒙哥甚至显示结果页面。相反,它抛出一个404我注意到这个错误来自于网址:
这是奇怪的,因为我想重定向到一个网页我呼吁return.html (它存在于正确的目录中,我测试过这个html是有效的)。这是我的控制器:
>@Controller
@RequestMapping("/articles")
public class ArticleController {
private ArticleRepo articleRepo;
@Autowired
public ArticleController(ArticleRepo articleRepo) {
this.articleRepo = articleRepo;
}
@RequestMapping(value = "/findAll", method = RequestMethod.GET)
@ResponseBody
public List<Article> findall() {
return articleRepo.findAll();
}
@GetMapping("/addArticle")
public String getAddArticle(Model model) {
model.addAttribute("article", new Article());
return "submitAnArticle";
}
@PostMapping("/addArticle")
public String submitAddArticle(@ModelAttribute Article newArticle) {
articleRepo.save(newArticle);
return "result";
}
}
我试着浏览堆栈溢出并使用this Spring文档以供参考,但我坚持。我对此有几个问题:
最重要的问题 1.谁能得到这个形式的工作或可能能够识别这个错误?
奖金问题 2.我已经有这个在Javascript中成功工作之前,我切换到Thymeleaf的怪异形式的东西。无论如何,我可以从这里采集数据并将其放入JavaScript中,就像我以前一样?我尝试使用标准的HTML表单的东西,但它没有做任何事情。 3.有没有一种方法可以在不通过AJAX重定向页面的情况下完成?
看来我用我的问题来堆栈溢出,因为它们一直是upvoted,但要么有不正确的答案,要么根本没有。任何帮助,将不胜感激。
_“任何人都可以得到这个形式工作” _ - 除非你提供整个应用程序。这是不会发生的,这个问题太过于宽泛,无关紧要。如果您可以提供证明问题的[mcve],那么也许我们可以提供帮助。 –