javascript - 使用 Stripes、Javascript 和 Ajax

标签 javascript ajax prototypejs stripes

这是我的 jsp 页面(这是另一个 jsp 页面的模态页面),其中包含一个表、一个表单、一个 javascript 和 ajax 。

<%@ include file="/WEB-INF/includes/taglibs.jsp" %>
<script type="text/javascript"
        src="${pageContext.request.contextPath}/ajax/prototype.js"></script>
<script type="text/javascript" xml:space="preserve">

function invoke(form, event, container) {        
        var params = Form.serialize(form, {submit:event});
        new Ajax.Updater(container, form.action, {method:'post', parameters:params});
    }
</script>

<display:table name="actionBean.currentAidApplicantYear.comments" id="result" class="maui">
    <display:column property="lastUpdatedBy" title="Last Updated By" sortable="true"/>
    <display:column property="lastUpdatedTimestamp" title="Last Updated Date"
                    format="{0,date,MM/dd/yyyy HH:mm}" sortable="true"/>
    <display:column property="comment" title="Memo"/>
</display:table>
<div class="actionBar" style="margin-top: 20px; text-align: center;">
    <stripes:form beanclass="${actionBean.class}" id="addMemoForm" method="POST">
                <tags:labelAndValue label="Comment" name="comment" >
                    <stripes:textarea id="commentTextArea" name="comment.comment" cols="75"/>
                </tags:labelAndValue>
        <stripes:submit name="saveCommentAjax" value="Add Memo"
                        onclick="invoke(this.form, this.name, 'result');"/>
        <stripes:hidden name="id" />        
    </stripes:form>  
</div>

这是操作 bean 的一部分,它扩展了另一个类,而该类又实现了 ActionBean、ValidationErrorHandler

Public class  CommentsTab extends AbstractAidApplicantTab {
private AidApplicantYearComment comment;

    public AidApplicantYearComment getComment() {
        return comment;
    }
    public void setComment(AidApplicantYearComment comment) {
        this.comment = comment;
    }
public Resolution saveCommentAjax(){
                    String result = String.valueOf(comment.getComment());
                    comment.save();//build up the comment object 
//by this time the comment object will save the string comment, user who updates it and a //time stamp. Those are the three variables that are displayed on the jsp table.
        return new StreamingResolution("text/html",new StringReader(result));}
//here instead of returning just a string “result” I prefer to return a comment object or //the three values I wanted to display on a table. How can I do that?

当单击提交按钮时,我使用 ajax 调用操作 bean 的方法来执行某些操作,并且该函数返回流分辨率 (StreamingResolution("text/html",new StringReader(result));) 。收到响应后,我想刷新表格而不刷新页面。但是,为了做到这一点,我必须从响应中获取一个对象(注释对象),而不是文本(或者可能是可能包含对象值的字符串数组)

如有任何帮助,我们将不胜感激。谢谢

最佳答案

使用 JavaScriptResolution ,或者(更好的恕我直言),

  • 将您的 Comment 对象转换为 JSON 字符串(使用无数可用的免费 Java JSON 编码器之一),
  • 将该 JSON 字符串作为 StreamingResolution 返回
  • 使用浏览器中的原生 JSON 函数(如果您的目标是最近的浏览器)或包含 JSON 解析函数的 JS 库,将 JSON 字符串转换为 JavaScript 对象。

关于javascript - 使用 Stripes、Javascript 和 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9330970/

相关文章:

javascript - 是否可以自动将 JavaScript 转换为 TypeScript?

mysql - Laravel 的 View 的 ajax 命名路由

javascript - 如何在我的 html 代码中定位特定的 .data

javascript - 使用 prototype.js,如何获取与对象关联的观察者列表?

javascript - Summernote的jQuery验证错误

javascript - 将过滤后的数据分配给 ng-if 中的 Controller 变量

javascript - 无法使用 jQuery AJAX 从服务器获取孟加拉语语言的数据

javascript - 带有原型(prototype)的多个 CSS 选择器

jquery - 如何在 jQuery 中显示加载微调器?

php - AJAX 登录表单重新加载页面