jquery - How to handle exception thrown by struts2 when call actions by ajax? -


i use jquery post method call save method in departmentaction, if save dup department(dup department not allowed in db), save method throws exception, user can't information page, here code:

departmentaction class:

public class departmentaction extends actionsupport{   public string save() throws exception    {     this.departmentservice.save(this.dept);     this.jsonresult = "success";     return "save";   }  } 

struts.xml:

<package name="backstage-default" extends="ecs-default" namespace="/manager">    <action name="dept_*" class="com.nader.action.departmentaction" method="{1}">       <result name="save" type="json"><param name="root">jsonresult</param></result>    </action>   <global-results>         <result name="error">/error.jsp</result>     </global-results>      <global-exception-mappings>         <exception-mapping exception="java.lang.exception" result="error"         />     </global-exception-mappings> 

test.jsp:

     $.post('dept_save.do',                   {'dept.deptid':$("#dialog_dept_deptid").val(),                    'dept.deptname':$("#dialog_dept_deptname").val(),                   'dept.manager':$("#dialog_dept_manager").val(),                  },function(data, status, xhr){                      alert(status);                      if(status == 'success'){                          alert('success:'+data);                          $("#dialog-form").dialog("close");                          getcountbyajax();                          getdeptbyajax();                      }else{                          alert('error:'+data);                      }                   }                  ,"json").fail(function(data, status, xhr)          {alert('fail:'+data);}); 

when save throws exception, user doesn't info, no errors, no redirecting, should do? don't want write try catch block in actions, because make code messy. there other ways?

thanks in advance.

ps: sorry, pasted wrong javascript block, correct it, fail handler executed when struts2 throws exception, don't know data passed browser is, , see response of dept_save.do in chrome tools, contains exceptionstack info, that's much, can struts returns small info or friendly tip info?

i don't want write try catch block in actions, because make code messy.

that nonsense argument. when components of framework using might throw exceptions, have catch if want keep control of programm flow , not have break on errors.

(you of course not have catch exception there , – exceptions can caught @ later points in program flow well. have caught @ somewhere before program ends.)


Comments