In my application user can create post,however only the creator of the post can delete it.
So I have to make the decision for each post when I list them.
Then which is better to to this logic decision,in the controller or in the view?
Now I do it in the view:
Controller:
class PostController{
@RequestMapping("post/list")
public void list(Model m){
List<Post> posts=queryPosts();
m.addAttribute(posts);
}
}
post_list.ftl
<#list ${posts} as post>
<span>${post.name}</span>
<#if ${post.owner}==${session.user}>
<a href="post/${post.id}/delete">Delete</a>
</#if>
</#list>
Then I want to know if this is the best practice?
If put the logic decesion in the controller is better,then how to make it? Add an editable field of the Post?
The final decision and logic about what item can be deleted by which user should be made in the controller. Ironically, from what I can see in your code this is actually what is happening – you are just making the determination about wether to show the delete link in your UI, based off the current user – which is fine in my opinion.