0

Good day!

I have a task to write a blog with Spring Boot and Thymeleaf. Nothing special, common CRUD operations. I have a problem sending data from one HTML page to another. Answer given here: Thymeleaf send parameter from html to controller doesn't work, and I can't understand why. May be someone could help me? Thank you!

My files: Entity:

@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.

Controller: (only problematic methods)

@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";
    }

My views: 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>

The problem is to send post id for selected post from showAllPosts to selectedPost.html. Invoking "showAllPosts" I obtain such kind of error:

There was an unexpected error(type=Internal Server Error, status=500). Error during execution of processor

'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "showAllPosts" - line 25, col 42)

(it's here: <input type="hidden" th:field="${id}" /> )

UPDATE. Here is the POST method for "selectedPage":

@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";
}

Update 2. A little bit more from stack trace: 2018-08-24 00:09:47.188 ERROR 12204 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "showAllPosts": Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "showAllPosts" - line 25, col 42)

org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "showAllPosts" - line 25, col 42) at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.engine.StandaloneElementTag.beHandled(StandaloneElementTag.java:228) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.engine.Model.process(Model.java:282) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]...

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'id' available as request attribute at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:153) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:257) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]...

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'id' available as request attribute at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:153) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:257) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]...

Sorry, if it's unuseful things...

8
  • Can you post a larger part of your stacktrace? Commented Aug 23, 2018 at 21:02
  • You use POST as method but your request mapping is GET. I would go for GET and use links instead of forms. selectedPost?id=12 or even more readable /posts for all posts and /posts/12 for one selected post. You can use path variables in spring for this Commented Aug 23, 2018 at 21:11
  • Changing POST to GET gave the same result. I have methods for GET and POST - does not work. I also tried path variables - the same result: methods don't get id. Commented Aug 23, 2018 at 21:15
  • I added stack trace logs (first lines of each block) at the bottom of the post. Commented Aug 23, 2018 at 22:47
  • try to change this <input type="hidden" th:field="${id}" /> line into <input type="hidden" th:field="${post.id}" /> Commented Aug 24, 2018 at 5:57

1 Answer 1

3

I think it is worth adding a proper answer. The correct way to collect bean properties in a html form in Thymeleaf 3.0 according to this is this form th:field="*{id}". Quote:

As you can see, we are introducing a new attribute here: th:field. This is a very important feature for Spring MVC integration because it does all the heavy work of binding your input with a property in the form-backing bean.

And if you already tried that try sending the id of the post as a parameter instead of the post object. Maybe the post object must exist in the model and cannot be picked up from the iteration. Like this:

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

And use this in the web method like this:

public String showMethod(@RequestParam(name="id", required=false) Integer postId, Model model)
Sign up to request clarification or add additional context in comments.

1 Comment

Then you should accept the answer. Glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.