简体   繁体   中英

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

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. I am doing an AJAX call.

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 .

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. How should I differentiate which jsp is there in data so that accordingly I alert('Updated DB') or load Error.jsp in errorDiv ??

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

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.

I mean if there was an Exception in bean I return error and accordingly in my strust.xml i return Error.jsp . So I know that control will come to success of AJAX request. After reaching there how to handle the 2 JSP?

Use below code to trace exception in ajax call:

    $.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. 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. Use a status code of 5XX for a server error. ( 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({
        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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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