Reputation: 3976
I use jquery post method to call save
method in DepartmentAction
, if I save a dup department(dup department is not allowed in db), the save
method throws an Exception, but user can't get any information about it from the page, here is my 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 an Exception, user doesn't get any info, no errors, no redirecting, so what should I do? I don't want to write try catch
block in Actions, because it is too much to make code messy. Are there some other ways?
Thanks in advance.
ps: I am really sorry, I just pasted the wrong javascript
block, I correct it, now the fail
handler was executed when struts2 throws an exception, but I don't know what the data
passed to browser is, and I see the response of dept_save.do
in chrome tools, it contains all exceptionStack info, that's too much, can struts returns some small info or friendly tip info?
Upvotes: 0
Views: 1332
Reputation: 96226
I don't want to write try catch block in Actions, because it is too much to make code messy.
That is a nonsense argument. When components of the framework you are using might throw exceptions, than you have to catch those if you want to keep control of the programm flow and not have it just break on errors.
(You do of course not have to catch that exception there and than – exceptions can be caught at later points in the program flow as well. They just have to be caught at all somewhere before the program ends.)
Upvotes: 1