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...
selectedPost?id=12or even more readable/postsfor all posts and/posts/12for one selected post. You can use path variables in spring for this<input type="hidden" th:field="${id}" />line into<input type="hidden" th:field="${post.id}" />