How can I make the Play action not redirect me to another page

I am using Play 2.3.1 with Java



I have a form that uses an action and should return Result ok().
My problem is that when it does return ok() it sends me to the URL mapped on the routes file, even though it should remain on the same page.



How can I fix this/What am I doing wrong?



my code:



The action



  public static Result postComment(){
CommentModel comment = Form.form(CommentModel.class).bindFromRequest().get();
comment.author = loggedUser;

if(!comment.content.isEmpty()){
return ok();
}
return null;

}


The route



POST    /postComment                controllers.Application.postComment()


The form



<form action="@routes.Application.postComment()" method="post">
<label for="comment" class=required>Your comment</label>
<textarea rows="2" name="content"></textarea>
<br/>
<button class="btn btn-primary">Comment</button>
</form>


Also, how can I make it so that when the action doesn't go into the 'if' nothing happens, since it always says that I need to return a Result? Is there a Result return that does nothing ?



Answers

"I have a form that uses an action and should return Result ok(). My problem is that when it does return ok() it sends me to the URL mapped on the routes file, even though it should remain on the same page."



I can't see anything wrong here. If you just return an OK / HTTP status code 200 your client/browser will get exactly just that. Play's routing happens before the action is called. Maybe you do something in your JavaScipt that gets activated after your form was submitted successfully?



"Also, how can I make it so that when the action doesn't go into the 'if' nothing happens, since it always says that I need to return a Result? Is there a Result return that does nothing ?"



You have to return a Result. But If you just want to tell your client side about a failed POST you can consider one of the 400er status code, e.g. 400 / Bad Request. In Play its return badRequest().