将值从ajax传递给struts标记

时间:2014-08-05 11:58:32

标签: javascript jquery json struts2

我正在使用以下ajax函数:

<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>

我想将结果传递给&#34; divComment&#34;,在div中使用ajax和dipslay获取值。我想在database中显示我的Comment表的所有属性。保存数据库中的值在div中显示这个值。我想在用户评论时在我的页面中添加评论,我想用ajax保存,因为我不想重新加载页面。

<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>

这是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>

这是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;
    }

这里是持续发表评论的CommentImpl.java

   @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;
}

如何在CommentImpl.java中返回JSON对象?我是JSON的新手。 谢谢!

1 个答案:

答案 0 :(得分:1)

您需要做的是,在您保存注释的操作中,在保存后将该对象作为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;
}

所以网址throw-exception/createComment.html?id=12&comment.description=somecomment应该返回评论对象。

假设json返回如下。

  

{&#34; ID&#34;:16,&#34;使用者&#34;:&#34;用户1&#34;&#34;评价&#34;:&#34; somecomment&#34; &#34;日期&#34;:&#34 1 /2014分之1&#34;&#34;条款ArticleID&#34;:12}

获得json后,您可以通过访问data

在ajax函数中使用它
 ....
 success: function (data) {

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