Ajax error struts2?

4.6k views Asked by At

I have in my action class:

try{

        tspNameIdMap = slsReqMgmtCommonRemote.getTspNameIdMap(Integer.parseInt(circleId));

        throw new ReqMgmtException("Message test");
    }
    catch(ReqMgmtException rEx){

        addActionError("Action-Error: Request Management Exception thrown");
        return ERROR;
        }

I am doing an AJAX call and using Struts2-Json-plugin to get tspNameIdMap in JSON form.

JS: Part of AJAX:

success: function(data){
                alert('Updated DB');
            },
            error: function(data){
                alert(data);
            }

My struts.xml:

<action name="findTspNameIdMap"
            class="cdot.oss.cmsat.gma.struts.ConfigureTspThresholdAction" method="findTspNameIdMap">
            <result name="success" type="json">
                <param name="root">
                    tspNameIdMap                    <!-- tspNameIdMap will be returned in JSON form -->
                </param>
            </result>
        </action>

I want my addActionError message in my Ajax error function. I will also want to display it in my JSP then. How can I get this?

Ps: Do I have to include this ActionError message in a properties file? I am using it for first time. Help

EDIT : As i followed @Prabhakar answer I got the error message, but it got returned in the success of the AJAX call.

Another issue is that, when i put this in my JSP, the actionerror is not getting displayed.

Jsp:

<s:if test="hasActionErrors()">
       <s:actionerror />
</s:if>
2

There are 2 answers

11
Roman C On BEST ANSWER

You can configure result ERROR to return the rendered error page fragment to the Ajax response.

<result name="error">/error.jspx</result>

error.jspx:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%
    request.setAttribute("decorator", "none");
    response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
    response.setHeader("Pragma","no-cache"); //HTTP 1.0
    response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
<s:if test="hasActionErrors()">
       <s:actionerror />
</s:if>
7
Prabhakar Manthena On

Please check the following

In your struts.xml edit the following

<result name="success" type="json">
  <param name="ignoreHierarchy">false</param>
    <param name="includeProperties">
        actionErrors.*,
        actionMessages.*,
        fieldErrors.*
    </param>
</result>

And in your js code edit the below.

    var actionErrs= (data.actionErrors); // list of action errors
    var actionMsgs= (data.actionMessages); // list of action messages
    var actionFldErrs= (data.fieldErrors); // list of field errors

   var alertMessage="";
    var i=0;
    for(i=0;i<actionErrs.length; i++){
    alertMessage=alertMessage+" "+actionMessages[i];
    }

    alert(alertMessage);

Edit in your JSP instead of the following,

<s:if test="hasActionErrors()">
       <s:actionerror />
</s:if>

use

<div id="err"></div>

and add below line in you js code

document.getElementById("err").innerHTML=alertMessage;

to check the result name

if your method signature like below,

public String getJsonData(){
if(....){
return ERROR;
}
return SUCCESS
}

You can get the data in your js code as below,

var actionErrs= (data.actionErrors); // list of action errors
var actionMsgs= (data.actionMessages); // list of action messages
var actionFldErrs= (data.fieldErrors); // list of field errors

var resultName=(data.getJsonData); // get The method return value

var alertMessage="";
var i=0;
for(i=0;i<actionErrs.length; i++){
alertMessage=alertMessage+" "+actionMessages[i];
}

alert(alertMessage);