【问题标题】:How to send data from one page to another using Thymeleaf如何使用 Thymeleaf 将数据从一页发送到另一页
【发布时间】:2019-01-30 05:03:53
【问题描述】:

美好的一天!

我的任务是使用 Spring Boot 和 Thymeleaf 编写博客。没什么特别的,常见的 CRUD 操作。 我在将数据从一个 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 列)

(在这里:&lt;input type="hidden" th:field="${id}" /&gt;

更新。这是“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:Bean 名称“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) ~[thymeleaf-spring5-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: Bean 名称“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) ~[thymeleaf-spring5-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]...

对不起,如果是没用的东西……

【问题讨论】:

  • 你能发布更多的堆栈跟踪吗?
  • 您使用 POST 作为方法,但您的请求映射是 GET。我会选择 GET 并使用链接而不是表单。 selectedPost?id=12 或更易读的 /posts 用于所有帖子,/posts/12 用于选定的帖子。您可以在 spring 中为此使用路径变量
  • 将 POST 更改为 GET 得到了相同的结果。我有 GET 和 POST 的方法 - 不起作用。我还尝试了路径变量 - 结果相同:方法没有获得 id。
  • 我在帖子底部添加了堆栈跟踪日志(每个块的第一行)。
  • 尝试将此&lt;input type="hidden" th:field="${id}" /&gt; 行更改为&lt;input type="hidden" th:field="${post.id}" /&gt;

标签: spring-boot thymeleaf


【解决方案1】:

我认为值得添加一个正确的答案。根据this,在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>

并在 web 方法中像这样使用它:

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

【讨论】:

  • 那么你应该接受答案。很高兴我能帮上忙。
猜你喜欢
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
  • 2014-05-15
  • 1970-01-01
  • 2017-05-22
  • 2016-03-19
相关资源
最近更新 更多