春季百里香表格网址无法解析

时间:2018-09-26 01:35:19

标签: spring-mvc thymeleaf

我想知道如何在Spring Form MVC平台中传输参数。 首先,下面的代码是spring表格java文件。

public class PostForm {

    @NotNull
    @Size(max=30, message="type id within 30 limits")
    private String title;

    @NotNull
    @Size(max=100, message="type id within 100 limits")
    private String Content;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return Content;
    }

    public void setContent(String content) {
        Content = content;
    }
}

下一个文件是有限的edit.html文件

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8">
<title>Blog modification</title>
</head>
<body>

<h1>Please, Modifiy.</h1>
<form method="post" th:object="${postForm}">
    <div><label for="title">title</label></div>
    <input id="title" type="text" name="title" th:value="*{title}" />
    <span class="formError" th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Input title is wrong</span>

    <div><label for="content" th:value="*{title}">Content</label></div>
    <textarea name="content" rows="20" width="200" th:value="*{content}"></textarea>
    <span class="formError" th:if="${#fields.hasErrors('content')}" th:errors="*{content}">Input content is wrong</span>

    <div>
        <input type="submit" value="Modify" />
        <a href="index.html" th:href="@{/posts}">Cancel</a>
    </div>
</form>
</body>
</html>

表单的输入链接网址如下所示,

    <td>
        <a href="posts/edit.html" th:href="@{posts/edit/__${post.id}__}">edit</a><br/>
        <a href="posts/edit.html" th:href="@{posts/edit/__${post.id}__}">delete</a>
    </td>

但是在Spring MVC控制器代码中抛出了异常。

@RequestMapping("/posts/edit/{id}")
    public String edit(PostForm postForm) {
        return "posts/edit/{id}"; //This line throws exception.
    }

@RequestMapping(value="/posts/edit/{id}", method = RequestMethod.POST)
    public String edit(@PathVariable("id") Long id, @Valid PostForm postForm, BindingResult bindingResult) {

        Post p = postService.findById(id);
        postForm.setTitle(p.getTitle());
        postForm.setContent(p.getBody());

        .....

例外是

ERROR 4024 --- [nio-8080-exec-4] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-4] Exception processing template "posts/edit/{id}": Error resolving template "posts/edit/{id}", template might not exist or might not be accessible by any of the configured Template Resolvers

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "posts/edit/{id}", template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:870) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]

我不知道如何在Spring Thymeleaf表单模板中传递参数。

1 个答案:

答案 0 :(得分:1)

在Sprig MVC中,当使用GET方法调用@ReqeustMapping批注时,它将尝试查找具有在返回值中定义的名称的html模板。

@RequestMapping("/posts/edit/{id}")
public String edit(PostForm postForm) {
    return "posts/edit/{id}"; //This line throws exception.
}

所以在这里,您必须返回资源文件夹(而不是url)中html模板的名称

所以我想应该是

@RequestMapping("/posts/edit/{id}")
public String edit(PostForm postForm) {
    return "views/mytemplate";
}

该错误显然表明它在资源文件夹下找不到模板。您的代码要做的是尝试将百里香叶模板放在资源文件夹下名为“ {id}”的“ posts”文件夹下的“ edit”文件夹中,但该位置不存在,因此会引发错误。

我的建议是如上所述更改GET方法的返回值。

如果需要将任何参数传递给视图,请使用Model类。 如果必须从{id}计算参数的值,则可以使用@PathVariable将ID映射到参数。

@RequestMapping("/posts/edit/{id}")
public String edit(@PathVariable(value="id") String id, Model model) {
    // do something here to get values using the id
    ....

    model.addAttribute("parameter1", parameter1);

    return "views/mytemplate";
}

通过这种方式,您不需要GET方法中的PostForm参数,因为在调用它时,它不会在主体中传递任何postForm参数。您可以将其留空。

希望这会有所帮助,祝您编程愉快! :)