如何使用Thymeleaf

时间:2018-08-23 18:33:03

标签: spring-boot thymeleaf

美好的一天!

我的任务是使用Spring Boot和Thymeleaf编写博客。没什么特别的,常见的CRUD操作。 我将数据从一个HTML页面发送到另一HTML页面时遇到问题。在这里给出答案: Thymeleaf send parameter from html to controller 不起作用,我不明白为什么。可能有人可以帮助我吗?谢谢!

我的文件: 实体:

@Entity
@Table(name="PRIVATEBLOG")
public class BlogPost{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="Id", nullable = false)
    private Long id;

    @Column (name="Date", nullable = false)
    private LocalDate postDate;

    @Column (name="PostText", length = 960, nullable = false)
    private String postText;
setters& getters.

控制器:(仅存在问题的方法)

@RequestMapping(value={"/showAllPosts"}, method = RequestMethod.GET)
    public String showAllPosts(Model model){
        List<BlogPost> allBlogPosts = new ArrayList<>();
        blogPostDAO.findAll().forEach(b -> allBlogPosts.add(b));
        allBlogPosts.stream().collect(Collectors.toList());
        model.addAttribute("allBlogPosts", allBlogPosts);
        return "showAllPosts";
    }
@RequestMapping(value={"/selectedPost"}, method = RequestMethod.GET)
    public String showSelectedPost(Model model, @RequestParam Long id){
        BlogPost bp = blogPostDAO.findById(id).get();
        model.addAttribute("post",bp);
        return "selectedPost";
    }

我的观点: showAllPosts.html

<body>
      <h2>All posts in blog:</h2>
      
      <br/><br/>
      <div>
         <table border="1">
            <tr>
               <th>Date</th>
               <th>Post text</th>
               <th>Action</th>
            </tr>
            <tr th:each ="post : ${allBlogPosts}">
                <td th:utext="${post.postDate}">...</td>
                <td th:utext="${post.postText}">...</td>
                <td>
                <form th:action = "@{/selectedPost}"
                    th:object="${post}" method = "POST">
                    <input type="hidden" th:field="${id}" /> 
                <button type="submit">Select</button>
                </form>
                </td>
            </tr>
         </table>
      </div>
      <a href="index">Home</a>
   </body>

selectedPost.html

<body>
      <h2>Selected post:</h2>
      <br/>
      <h3>Date: </h3>
      <h3 th:utext="${post.postDate}">...</h3>
      <h3>Post: </h3>
      <h3 th:utext="${post.postText}">...</h3>
      <br/>
      <form th:action = "@{/deletePost}"
	 	   th:object = "${post}" method = "POST">
          <input type="hidden" th:field="${id}">
          <button type="submit">Delete</button>
      </form>
      <form th:action = "@{/updatePost}"
			   th:object = "${post}" method = "POST">
			   <input type="hidden" th:field="${id}">
			   <button type="submit">Update</button>
      </form>
      <a href="index">Home</a>
   </body>

问题是将选定帖子的帖子ID从showAllPosts发送到selectedPost.html。调用“ showAllPosts”时,我会收到此类错误:

  

发生意外错误(类型=内部服务器错误,状态= 500)。   处理器执行期间发生错误

     

'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'   (模板:“ showAllPosts”-第25行,第42栏)

(在这里:<input type="hidden" th:field="${id}" />

更新。这是“ selectedPage”的POST方法:

@RequestMapping(value={"/selectedPost"}, method = RequestMethod.POST)
    public String getSelectedPost(Model model, @ModelAttribute("post") BlogPost post){
        BlogPost bp = blogPostDAO.findById(post.getId()).get();
        BlogPostForm blogPostForm = new BlogPostForm();
        blogPostForm.setId(bp.getId());
        blogPostForm.setPostDate(bp.getPostDate());
        blogPostForm.setPostText(bp.getPostText());
        model.addAttribute("post",bp);
        return "selectedPost";
}

更新2。 来自堆栈跟踪的更多信息: 2018-08-24 00:09:47.188错误12204 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine:[THYMELEAF] [http-nio-8080-exec-1]异常处理模板“ showAllPosts” :执行处理器“ org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor”时出错(模板:“ showAllPosts”-第25行,第42行)

org.thymeleaf.exceptions.TemplateProcessingException:执行处理器'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'时出错(模板:“ showAllPosts”-第25行,第42行)     在org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]     在org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]     在org.thymeleaf.util.ProcessorConfigurationUtils $ ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]     在org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]     在org.thymeleaf.engine.StandaloneElementTag.beHandled(StandaloneElementTag.java:228)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]     在org.thymeleaf.engine.Model.process(Model.java:282)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] ...

原因:java.lang.IllegalStateException:BeanResult'id'的BindingResult或普通目标对象都不能用作请求属性     在org.springframework.web.servlet.support.BindStatus。(BindStatus.java:153)〜[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]     在org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903)〜[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]     在org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]     在org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305)〜[thymeleafspring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]     在org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:257)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] ...

java.lang.IllegalStateException:BeanResult'id'的BindingResult和普通目标对象都不能用作请求属性     在org.springframework.web.servlet.support.BindStatus。(BindStatus.java:153)〜[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]     在org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903)〜[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]     在org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]     在org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305)〜[thymeleafspring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]     在org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:257)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] ...

对不起,如果没用的话...

1 个答案:

答案 0 :(得分:0)

我认为值得添加一个正确的答案。根据{{​​3}}在Thymeleaf 3.0中以html形式收集Bean属性的正确方法是这种形式th:field="*{id}"。引用:

  

如您所见,我们在此处引入了一个新属性:th:field。这对于Spring MVC集成来说是非常重要的功能,因为它完成了将输入与表单支持bean中的属性绑定在一起的所有繁重工作。

如果您已经尝试过,请尝试发送帖子的ID作为参数而不是帖子对象。也许post对象必须存在于模型中并且不能从迭代中获取。 像这样:

<form th:action = "@{/selectedPost(id=${post.id})}" method = "POST">
                 <button type="submit" name="select">Select</button>
            </form>

并在如下所示的网络方法中使用它:

public String showMethod(@RequestParam(name="id", required=false) Integer postId, Model model)
相关问题