In this fourth tutorial we are going to customize the comment page.

If you haven't, read tutorial part 1part 2part 3.

The aim is to join the "create" and "search"page", this offers the possibility to read and create comments in the same page.
The goal is show in the following picture.

Step 1. Create a single page to list and create comments

Let's edit the "Groovy" action of the "comment" page  for fowarding to a custom jsp. This jsp will show the list and a form for creating comments.
@SupportsPermissions([ CrudAction.PERMISSION_CREATE, 

@RequiresPermissions(level = AccessLevel.VIEW)

class MyCrudAction extends CrudAction {
  // List objects

  public Resolution execute() {
      if (object == null) {
          return doList();
      } else {
          return read();

  public Resolution doList(){
     String jsp = "/apps/default/web/listComments.jsp";
      return new ForwardResolution(jsp);

The custom jsp page displays a list of comments and allows you to add new ones as shown in the following code.

<%@ page import="java.util.List" %>
<%@ page import="com.manydesigns.elements.xml.XhtmlBuffer" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="javax.xml.crypto.Data" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java"
<%@ taglib prefix="c" 
<%@ taglib prefix="stripes" 
<%@taglib prefix="mde" uri="/manydesigns-elements"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="portofino" %>
<%@taglib prefix="fmt" uri="" %>
<stripes:layout-render name="/skins/${skin}/portlet.jsp">
    <jsp:useBean id="actionBean" scope="request" 
    <stripes:layout-component name="portletTitle">
        <c:out value="${actionBean.crudConfiguration.searchTitle}"/>

    <stripes:layout-component name="portletBody">
           SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
           XhtmlBuffer buffer = new XhtmlBuffer();
           for (HashMap object : (List<HashMap>)actionBean.objects){
               buffer.write((String) object.get("author"));

            <h1>Add a new comment</h1>
        <form enctype="multipart/form-data" 
           action="<%= ((HashMap)actionBean
            <fieldset class="mde-form-fieldset mde-no-legend">
                <table class="mde-form-table">
                        <th>* Comment</th>
                        <td><textarea id="comment" type="text" 
                              name="comment" class="mde-text-field"
                              maxlength="2147483647" cols="45" rows=5>
                        <th><label for="author" class="mde-field-label">
                            <span class="required">*</span>
                            <input id="author" type="text" name="author" 
                              maxlength="100" size="45">

            <input type="hidden" name="cancelReturnUrl" 
    value="/post/<%= ((HashMap)actionBean.getOgnlContext()
            <input type="hidden" name="post" 
    value="<%= ((HashMap)actionBean.getOgnlContext()
            <input type="submit" value="Add comment" name="save" 
    class="ui-button ui-widget ui-state-default ui-corner-all 
           portletButton ui-button-text-only" 
    role="button" aria-disabled="false"/>

The section for creating a new comment is of particular interest. The form builds the action link with the id of the post and the String "comment". This is done to address the crud action of comments and not the one of post (the one where form is placed).

The fields have the properties names to be mapped to the Crud object. The submit tag has "save" for the property "name", this bind the submit to the method "save" in the CrudAction.
We can test our application, but we soon notice that we have the comments listed in the post page, but, after we save a comment,  we are directed to the detail page of the comment.
We can change this behavour. Let's ovveride the "save" method to forward to the parent page (the post to which the comment is linked). In the following code I'll use  the parent path through the pageInstance object.
//Save redirects to blog post
@Button(list = "crud-create", key = "", order = 1d)
@RequiresPermissions(permissions = AbstractCrudAction.PERMISSION_CREATE)
public Resolution save() {;
        .addErrorMessage("Please insert the Comment and the Author fields");
  return new RedirectResolution(pageInstance.parent.path);


We note also that we manually run the validation to print an error message on the parent "post" page.
With this last step we achieved the desired goal. And, finally, we have a fully functional blog with an attractive layout.
In the next tutorial we will conclude the series, we will create an rss feeds for our blog.

comments powered by Disqus