简体   繁体   English

在AJAX响应中返回JSP?

[英]Return JSP in AJAX response?

I have an Action class which has method which calls a Bean and sets some data in the DB based on some inputs. 我有一个Action类,该类具有调用Bean并根据一些输入在DB中设置一些数据的方法。

Action class : 动作类

try{
    slsConfigureTspThresholdRemote = this.getSlsConfigureTspThresholdRemote();
    slsConfigureTspThresholdRemote.setThresholdParameters(circleId, tspId, thresholdTypeFlag, thresholdParametersList);
}
catch (Exception e){    
    addActionError(e.getMessage());
    e.printStackTrace();
    System.out.println("[AnalysisStatisticsAction] updateThresholdParameters: In catch Inside Constructor!!");
    return ERROR;
}
return DISPLAY;

If there is an ERROR i return an Error.jsp where actionError are displayed and if DISPLAY then the same input page is returned. 如果有ERROR我将返回一个Error.jsp ,其中会显示actionError ;如果是DISPLAY则会返回相同的输入页面。

strust.xml : strust.xml

<action name="updateThresholdParameters"
            class="cdot.oss.cmsat.gma.struts.ConfigureTspThresholdAction" method="updateThresholdParameters">

            <result name="display">pages/ConfigureTspThresholdInput.jsp</result>
            <result name="error">pages/Error.jsp</result>

        </action>

I use strust2-json-plugin. 我使用strust2-json-plugin。 I am doing an AJAX call. 我正在拨打AJAX电话。

JS : JS

 $.ajax({
                type: 'POST',
                traditional: true,                  
                url: '/gma/updateThresholdParameters.action',
                data:
                {
                    circleId: circleId,
                    tspId: tspId,
                    thresholdTypeFlag: thresholdTypeFlag,
                    thresholdParameters: thresholdParameters
                },
                success: function(data){
                     alert('Updated DB');
                },
                error:function(data){
                    alert("something is not fine!!!");
                }
         });

So if data is updated in DB i alert('Updated DB') or if exception then I want my Error.jsp to be loaded in ErrorDiv . 因此,如果数据在DB i alert('Updated DB')或者如果发生异常,那么我希望将我的Error.jsp加载到ErrorDiv

So the problem is: 所以问题是:

If no exception occurs in my data then ConfigureTspThresholdInput.jsp is returned and if exception is there then Error.jsp is returned in data. 如果我的data没有异常,则返回ConfigureTspThresholdInput.jsp ,如果有异常,则在数据中返回Error.jsp How should I differentiate which jsp is there in data so that accordingly I alert('Updated DB') or load Error.jsp in errorDiv ?? 我应该如何区分哪个JSP是存在的数据,这样相应的我alert('Updated DB')或加载Error.jsperrorDiv

I tried searching online. 我尝试在线搜索。 I read about statusCode and ErrorCode of JSON plugin, but with that I don't know how data part is filled(I alert and got an XML doc). 我读到有关JSON插件的statusCodeErrorCode的信息,但是我不知道如何填充数据部分(我发出警报并获得了XML文档)。

EDIT : From the answers I guess people are misunderstanding the question. 编辑 :从答案我想人们误解了这个问题。 So let me clear. 所以让我清楚。

When I say exception I am not saying that AJAX request itself could not be done or it failed. 当我说异常时,我并不是说AJAX请求本身无法完成或失败。

I mean if there was an Exception in bean I return error and accordingly in my strust.xml i return Error.jsp . 我的意思是,如果bean中存在异常,我将返回error ,因此在我的strust.xml我将返回Error.jsp So I know that control will come to success of AJAX request. 因此,我知道控制将success实现AJAX请求。 After reaching there how to handle the 2 JSP? 到达那里后如何处理2个JSP?

Use below code to trace exception in ajax call: 使用以下代码来跟踪ajax调用中的异常:

    $.ajax({
                type: 'POST',
                traditional: true,                  
                url: '/gma/updateThresholdParameters.action',
                data:
                {
                    circleId: circleId,
                    tspId: tspId,
                    thresholdTypeFlag: thresholdTypeFlag,
                    thresholdParameters: thresholdParameters
                },
                success: function(data){
                     alert('Updated DB');
                },
                error:function(data){
                    alert("something is not fine!!!");
                }
         });

I am not a 100% sure I understand your question. 我不是100%肯定知道您的问题。 What I get from it is: 我从中得到的是:

You want your site to alert "Updated DB" if there was no error and everything went smooth; 您希望您的站点在没有错误且一切顺利的情况下提醒“更新的数据库”。 else you want your site to show an error page? 否则您希望您的网站显示错误页面?

You should use HTTP Status codes to report wether the request went good or bad. 您应该使用HTTP状态代码来报告请求的好坏。 Use a status code of 5XX for a server error. 对于服务器错误,请使用状态码5XX。 ( http://en.wikipedia.org/wiki/List_of_HTTP_status_codes ) http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

When your ajax request receives an error status code it will call the error function: 当您的ajax请求收到错误状态代码时,它将调用错误函数:

$.ajax({
        type: 'POST',
        traditional: true,                  
        url: '/gma/updateThresholdParameters.action',
        data:
        {
            circleId: circleId,
            tspId: tspId,
            thresholdTypeFlag: thresholdTypeFlag,
            thresholdParameters: thresholdParameters
        },
        success: function () {
            alert("DB updated")
        },
        error: function () {
            Redirect to your error page instead of returning it.
        }
    });

I hope this is what you wanted. 希望这就是您想要的。

You should use Shorthands to keep code clean. 您应该使用简写方式来保持代码干净。

$('#OkDiv').load('/gma/updateThresholdParameters.action',{
  circleId: circleId,
  tspId: tspId,
  thresholdTypeFlag: thresholdTypeFlag,
  thresholdParameters: thresholdParameters
},function(a,c,xhr){if(c=='error')$('#ErrorDiv').html(xhr.responseHTML);});
  1. Its automatically POST 它自动开机自检
  2. Its automatically traditional 其自动传统
  3. Its automatically replaced 自动替换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM