【问题标题】:Working with Stripes, Javascript and Ajax使用 Stripes、Javascript 和 Ajax
【发布时间】:2012-02-17 15:29:16
【问题描述】:

这是我的 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));) 。一旦我得到响应,我想刷新表格而不刷新页面。但是,为了做到这一点,我必须从响应中获取一个对象(评论对象),而不是文本(或者可能是可能包含对象值的字符串数组)

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: javascript ajax prototypejs stripes


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复。我以前从未使用过 JSON。我会尝试任何方法。
    • 你认为使用 JavaScriptResolution 和 JSON 哪个更容易?
    • 它们都很简单。 JSON 是标准的,不需要使用 eval()。
    • 我开始使用 JSON,它抛出“层次结构中有一个循环”......为了解决这个问题,我发现其他文章提到了使用 JSONConfig......这是其中之一链接albert-myptc.blogspot.com/2011/09/… ...但我无法理解他试图用他的解决方案做什么..特别是 config.setExcludes 部分令人困惑...我以为这家伙正在使用数据库表列名,我做了同样的事情然后我收到另一个错误,上面写着 java.sql.SQLExcpetion Unsupported feature。有什么线索我可以解决它吗?谢谢
    • 您只有三个必须发送的字段。我会简单地使用具有三个属性的专用简单对象并将该对象序列化为 JSON,或者使用允许显式创建 JSON 对象并且不序列化 Java 对象的库。
    猜你喜欢
    • 2014-09-06
    • 2010-11-29
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    相关资源
    最近更新 更多