1

I am using following ajax function:

<script>
$(document).ready(function () {
    $('#divComment').hide();
    $("#comment_submit").click(

    function (event) {
        jQuery.ajax({
            type: 'GET',
            url: 'throw-exception/createComment.html?id=' + $('#id').val(),
            data: {
                'comment.description': $("#comment").val()
            },
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {

                $('#divComment').append("<div class='commentscontainer'>" + $("#user").val() + "<br/>" + $("#comment").val() + "</div><br/>");
                $('#divComment').slideDown();
                $('#comment').val('');
            },
        });
    });
});
</script>

I want to pass the result into a "divComment",get value with ajax and dipslay in a div.I want to display all the property of my Comment table in database.Save the value in database and display this value in a div.I want to add comment in my page when a user comment,i want to save with ajax because i don't want to reload page.

<article class="page">
    <header class="throwException-header">
            <h1 class="throwException-title"><s:property value="throwException.title" /></h1>
    <span class="throwException-user"><s:property value="throwException.user.accountName" /></span> | <span class="throwException-created"><s:property value="throwException.created" /></span>
    </header>
    <p><s:property value="throwException.description" />
    </p> <a id="displayText">Add a comment</a>
    <div id="toggleText">
        <div class="form-group"><s:hidden id="id" name="id" /><s:property value="user.accountName" />
            <br /><s:textarea cssClass="form-control" row="15" cols="140" id="comment" name="comment.description" />
        </div>
        <button class="btn btn-primary" id="comment_submit">Comment</button>
        <br/>
    </div>
    <div class="commentscontainer">
        <s:if test="listComment.size() > 0">
            <s:iterator value="listComment" status="status">
                <article class="throwException-item <s:if test=" #status.odd==t rue ">odd</s:if><s:else>even</s:else>">
                    <div><s:property value="user.accountName" />
                        <br /><s:property value="description" />
                        <br />
                    </div>
                </article>
            </s:iterator>
        </s:if>
        <div id="divComment" class="commentscontainer"></div>
    </div>
</article>

Here is struts.xml

<package name="default" namespace="/throw-exception"  extends="struts-default,json-default">

    <action name="createComment" method="createComment" class="action.CommentService">
<result name="success" type="redirect">listComment.html</result>
</action>


<action name="listComment" method="listComment" class="action.CommentService">
            <result type="json" />
        </action>


        </package>

Here is CommentService.java:

public String createComment() {
        logger.info(Logger.EVENT_SUCCESS,
                "beggin");
        try {
            logger.info(Logger.EVENT_SUCCESS,
                    "inside");
            facade.createComment(getComment());


        } catch (Exception e) {

            logger.error(Logger.EVENT_FAILURE,
                    "could not create comment , error: *" + e.getMessage()
                            + "*");

        }

        return SUCCESS;
    }

And here is CommentImpl.java that persist my comment

   @Transactional(readOnly = false)
    @Override
    public void createComment(Comment theComment) {

                Comment comment = new Comment();
                comment.setUser(user);
                comment.setDescription(theComment.getDescription());
               // comment.setReplyCommentId(theComment.getReplyCommentId());
                comment.setDate(new Date());
                em.persist(comment);
                em.flush();
                return comment;
}

How do I return a JSON object in my CommentImpl.java?I am new in JSON. Thanks!

Fation Hadri
  • 160
  • 2
  • 2
  • 20
  • Do you have some question or not? – Roman C Aug 05 '14 at 12:46
  • How to pass all the property of my Comment table in database in "divComment"?Any other function in javascript to pass value? – Fation Hadri Aug 05 '14 at 12:48
  • For this you should have a schema in database and account on which you can connect and execute SQL. – Roman C Aug 05 '14 at 12:58
  • I have schema in database and ralationship but i have probelem in view.jsp.I save the data in database and i want this data to display in a div after saving in database,and i want to do this with ajax,but i can't get all the data from database.Thank you! – Fation Hadri Aug 05 '14 at 14:33
  • Why you can't get it if you have a comment table and id of the comment? Do you want return JSON on success callback? What is the JSON? – Roman C Aug 05 '14 at 15:18
  • I can't catch value of other property of table comment with ajax and i can't display this in a div.For the moment i display only the description of the comment in the div,not the name of the user,and the date when comment is created.How to catch this this value(name of the user and date of the comment) that are save autamtically in database when user submit a comment.I want a JSON back but display not only the description of the comment.JSON is a string that have some value on it – Fation Hadri Aug 05 '14 at 15:34
  • may be you can get some help from this http://stackoverflow.com/questions/15461490/delete-user-from-a-list-using-ajax-and-refresh-list-only-using-struts-2/15549220#15549220 – Code2Interface Aug 06 '14 at 12:00

1 Answers1

1

What you need to do is, in action where you are saving comment, after the saving return that object as json.

@Transactional(readOnly = false)
@Override
public Comment createComment(Comment theComment) {

            Comment comment = new Comment();
            comment.setUser(user);
            comment.setDescription(theComment.getDescription());
           // comment.setReplyCommentId(theComment.getReplyCommentId());
            comment.setDate(new Date());
            em.persist(comment);
            em.flush();
        return comment;
}

so the url throw-exception/createComment.html?id=12&comment.description=somecomment should return you the comment object.

Suppose json returned is as below.

{"id":16,"user":"user1","comment":"somecomment","date":"1/1/2014","articleId":12}

Once you get json, you can use it in ajax function by accessing data,

 ....
 success: function (data) {

 $('#divComment').append("<div class='commentscontainer'>" + data.user + "<br/>" + $("#comment").val() + "date:"+data.date+"</div><br/>");
 $('#divComment').slideDown();
 $('#comment').val('');
 },
 ....
prem30488
  • 2,828
  • 2
  • 25
  • 57
  • I edit my question based on what you explain me but i can't return a json object to get data when i persist comment.Any code that help me?Thank you! @ParthTrivedi – Fation Hadri Aug 11 '14 at 15:42
  • i updated how to get persisted object.. now in your service code for json. – prem30488 Aug 11 '14 at 16:24
  • I edited again my question. It return me only comment description,not other property value. Thank you for your time :)My idea is like facebook comment,while we comment the div dispaly with comment value. @ParthTrivedi – Fation Hadri Aug 11 '14 at 19:19