2017-08-13 81 views
0

我在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我注意到这个错误来自于网址:

http://localhost:8080/addArticle

这是奇怪的,因为我想重定向到一个网页我呼吁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,但要么有不正确的答案,要么根本没有。任何帮助,将不胜感激。

+0

_“任何人都可以得到这个形式工作” _ - 除非你提供整个应用程序。这是不会发生的,这个问题太过于宽泛,无关紧要。如果您可以提供证明问题的[mcve],那么也许我们可以提供帮助。 –

回答

3

样本它肯定会抛出404错误。在您的控制器,您将可以在

@RequestMapping("/articles") 
public class ArticleController 

@PostMapping("/addArticle") 
public String submitAddArticle(@ModelAttribute Article newArticle) { 

映射两次,所以你的URL将被

http://localhost:8080/articles/addArticle 

,但是你想在

http://localhost:8080/addArticle 
+0

...那么你如何解决这个问题? – DaveCat

+1

试试这个。在你的表单中改变'th:action =“@ {/ addArticle}”'到'th:action =“@ {/ articles/addArticle}”'或从控制器中移除@RequestMapping(“/ articles”)''。 – budthapa

2

看地图,是不是你的URL http://localhost:8080/articles/addArticle,尝试删除“/”从动作:

th:action="@{addArticle}"

否则这可能会导致URL从根即服用:

http://localhost:8080/addArticle 

编辑:另外,你说:

,因为我想重定向到一个网页我呼吁return.html

但在你的控制器,你正在返回result,尝试将其更改为:根据你的代码

@PostMapping("/addArticle") 
public String submitAddArticle(@ModelAttribute Article newArticle) { 
    articleRepo.save(newArticle); 
    return "return"; 
} 
+0

这绝对需要我到正确的URL,但它仍然说404. 我注意到,即使每当错误不是真正的404,Thymeleaf也很频繁地抛出404s。例如,如果页面的html不是格式化以便Thymeleaf可以识别,它只会抛出一个404,但在文档中显示有问题的行。 – DaveCat

+0

看看我的编辑 –

+0

这是我的错字。我试图返回“结果”,但它没有这样做。 – DaveCat

0

我发布认为你可能不需要进口必需品Ÿ罐子,把这些在pom.xml中或下载自己

<dependency> 
     <groupId>nz.net.ultraq.thymeleaf</groupId> 
     <artifactId>thymeleaf-layout-dialect</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>net.sourceforge.nekohtml</groupId> 
     <artifactId>nekohtml</artifactId> 
    </dependency>