用ajax提交form可以和好的返回异常信息进行提示,免得做全局的异常处理,这样的话不必throw Exception
,直接像正常一样返回错误信息即可,此时结合response.getWriter().print(strJsonMsg);用这个的时候最好返回void,或者return null,否则print的信息会被覆盖。
一般用一个对象封装返回信息,返回前把这个对象转化为jsonobject,然后转化为String打印给前台(bean--->jsonobject--->String)
throw new Exception("最多允许添加:"+((BigDecimal)userNum.get("SET_USER_NUM")).intValue()+"个子账户");异常里面抛出的信息可以e.getMessage()直接获取到
<form method="post" id="operateUserAddForm">
<div class="htbh">
<div class="fl">
<label for="" class="ul_form_label w130 pr5"><font color="red">* </font>姓名:</label>
<input class="date_search_input230" type="text" name="man" id="cusUser_MAN"/>
</div>
</div>
<!--button 开始-->
<div class="tc mb20 mt20 cb">
<!-- 获取可选角色个数,为使能/失能 保存按钮服务-->
<%-- <input type="hidden" id="role_notice" value='<s:property value="#request.datas.size()"/>'/> --%>
<input name="input" id="saveBtn" type="button" onclick="add();" class="btnhr30 mr5" value="保 存"/>
<input name="" type="reset" class="btnhh30 ml5" value="重置" />
<!-- <s:reset cssClass="btnhh30 ml5" value="重置"></s:reset> -->
</div>
</form>
//添加保存
function add(){
if(!check()){
return false;
}
//alert(66);
$("#saveBtn").attr("disabled",true);
$("#saveBtn").removeClass("btnhr30");
$("#saveBtn").addClass("btnhh30");
$.ajax({
url:"${path}/myaccount/addUser",
type:"post",
cache:false,
async:true,
dataType:"json",
data:$("#operateUserAddForm").formSerialize(),
success:function(ret){
//alert(00);
if(ret.status==0){
//alert(11);
$.zd.alert("","【"+$("#cusUser_MAN").val()+"】操作员信息创建成功!",function(){
window.location.href="${path}/myaccount/getAccount";
});
}else{
//alert(22);
$.zd.alert('',ret.msg,function(){
$("#saveBtn").removeAttr("disabled");
$("#saveBtn").removeClass("btnhh30");
$("#saveBtn").addClass("btnhr30");
});
}
},
error:function(retMsg){
//alert(33);
//alert(retMsg.status);
//alert(retMsg.msg);
//displayProp(retMsg);
try{
//var ret = eval("("+retMsg.responseText+")");
if(retMsg.status!=0){
$.zd.alert('',retMsg.msg,function(){
$("#saveBtn").removeAttr("disabled");
$("#saveBtn").removeClass("btnhh30");
$("#saveBtn").addClass("btnhr30");
});
}
}catch(eee){
}
}
});
}
@RequestMapping(value = "/addUser")
public String addUser(Model model,TbCusUserBeanVo tbCusUserBeanVo, @RequestParam(value="rolesKey") String rolesKey,@RequestParam(value="pz") String[] pz,HttpServletRequest request,HttpServletResponse response, HttpSession session) throws Exception {
CusUserBean getCusUser= CASUtil.getCusUser(request, session);
if(tbCusUserBeanVo==null){
return null;
}
tbCusUserBeanVo.setCustomerKey(BigDecimal.valueOf(Long.valueOf(getCusUser.getCustomerKey())));
try{
Map<String,Object> param =new HashMap<String,Object>();
param.put("CUSTOMER_KEY", tbCusUserBeanVo.getCustomerKey());
Map userNum =accountService.getUserNum(param);
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
if(userNum==null){
accountService.addOperateUser(tbCusUserBeanVo, rolesKey, pz);
JsonMsg msg= new JsonMsg("0","添加成功");
String strJsonMsg = JSONUtils.toJSONString(JSONObject.fromObject(msg));
response.getWriter().print(strJsonMsg);
response.getWriter().flush();
}else{
int i =((BigDecimal)userNum.get("COUNT")).intValue();
if(((BigDecimal)userNum.get("SET_USER_NUM")).intValue()<i+1){
JsonMsg msg= new JsonMsg("-1","最多允许添加:"+((BigDecimal)userNum.get("SET_USER_NUM")).intValue()+"个子账户");
String strJsonMsg = JSONUtils.toJSONString(JSONObject.fromObject(msg));
response.getWriter().print(strJsonMsg);
response.getWriter().flush();
// throw new Exception("最多允许添加:"+((BigDecimal)userNum.get("SET_USER_NUM")).intValue()+"个子账户");
}else{
accountService.addOperateUser(tbCusUserBeanVo, rolesKey, pz);
JsonMsg msg= new JsonMsg("0","添加成功");
String strJsonMsg = JSONUtils.toJSONString(JSONObject.fromObject(msg));
response.getWriter().print(strJsonMsg);
response.getWriter().flush();
}
}
}catch(Exception e){
JsonMsg msg= new JsonMsg("-1",e.getMessage());////异常里面抛出的信息可以e.getMessage()直接获取到
String strJsonMsg = JSONUtils.toJSONString(JSONObject.fromObject(msg));
response.getWriter().print(strJsonMsg);
response.getWriter().flush();
e.printStackTrace();
return null;
}
return null;
}
JSONUtils.java
public static String toJSONString(JSONObject jsonObject) {
return jsonObject.toString();
}